当前位置:网站首页>new IntersectionObserver 使用笔记
new IntersectionObserver 使用笔记
2022-07-04 20:58:00 【爱前端不爱恋爱】
MDN API
随着网页发展,对检测某个(些)元素是否出现在可视窗相关的需求越来越多了?比如:
当页面滚动时,懒加载图片或其他内容。
实现“可无限滚动”网站,也就是当用户滚动网页时直接加载更多内容,无需翻页。
对某些元素进行埋点曝光
滚动到相应区域来执行相应动画或其他任务。
一直以来,检测元素的可视状态或者两个元素的相对可视状态都不是件容易事,大部分解决办法并不完全可靠,实现方式很丑陋,也极易拖慢整个网站的性能。
IntersectionObserver正因此而生
需求 : 对某些元素进行埋点曝光
reportDatail() 方法 对容器内容进行监听
如果dom 初始化存在 则在mounted() 进行监听即可;
如果 dom 是组件 是根据 请求返回的data 然后再渲染组件dom 则该方法使用在请求返回后
asiosDemo(){
...请求
if(success) {
this.list = data //返回的数据 然后list 对组件进行渲染
this.$nextTick(() => {
this.reportDatail()
})
}
}
methods:{
reportContent(index, name) {
console.log('曝光内容请求', index, name)
},
// 对dom 进行监听
reportDatail() {
// 容器dom
const dom = document.querySelector('.monitor-list')
setTimeout((_) => {
this.$_reportContent(
{
observeNodes: dom,
childNodes: dom.querySelectorAll('.content-task-item') // 监听子元素dom
},
// 回调函数 对返回的数据做埋点处理
this.reportContent
)
}, 0)
},
$_reportContent(options, callBackFn) {
// observeNodes 容器元素dom
// childNodes 被监听的曝光子元素dom
const {
observeNodes, childNodes } = options
const io = new IntersectionObserver(
function(entrier) {
entrier.forEach((el, i) => {
if (el.isIntersecting) {
// _index _name 是子元素dom 创建的值
let _index = el.target.getAttribute('data-index')
let _name = el.target.getAttribute('data-name')
callBackFn(_index, _name)
// 监听的元素进行解绑
io.unobserve(el.target)
}
})
},
{
root: observeNodes,
rootMargin: '0px'
}
)
if (childNodes && childNodes.length !== 0) {
childNodes.forEach((el) => {
let _index = el.getAttribute('data-index') * 1
// this.exposureList记录最新的曝光内容 之前曝光过的不再监听
if (this.exposureList.includes(_index)) {
io.observe(el)
}
})
}
},
}
边栏推荐
- minidom 模塊寫入和解析 XML
- Flutter在 release版本,打开后随机白屏不显示内容
- Jerry's ad series MIDI function description [chapter]
- redis RDB AOF
- redis管道
- Maidong Internet won the bid of Beijing life insurance
- WGCNA analysis basic tutorial summary
- Jerry's ad series MIDI function description [chapter]
- Jerry's ad series MIDI function description [chapter]
- 类方法和类变量的使用
猜你喜欢

Difference between ApplicationContext and beanfactory (MS)

Redis 排查大 key 的3种方法,优化必备

gtest从一无所知到熟练使用(3)什么是test suite和test case

解析互联网时代的创客教育技术

Huawei ENSP simulator enables devices of multiple routers to access each other

Huawei ENSP simulator realizes communication security (switch)

每日一题-LeetCode1200-最小绝对差-数组-排序

Can be displayed in CAD but not displayed in print
![Compréhension approfondie du symbole [langue C]](/img/4b/26cf10baa29eeff08101dcbbb673a2.png)
Compréhension approfondie du symbole [langue C]

TCP shakes hands three times and waves four times. Do you really understand?
随机推荐
Le module minidom écrit et analyse XML
Exclusive interview of open source summer | new committer Xie Qijun of Apache iotdb community
Master the use of auto analyze in data warehouse
Configuration of DNS server of Huawei ENSP simulator
Hash table
Case sharing | integrated construction of data operation and maintenance in the financial industry
gtest从一无所知到熟练使用(4)如何用gtest写单元测试
【活动早知道】LiveVideoStack近期活动一览
Huawei ENSP simulator configures ACL access control list
Flutter 返回按钮的监听
超详细教程,一文入门Istio架构原理及实战应用
Maya lamp modeling
IIC (STM32)
Monitor the shuttle return button
In the release version, the random white screen does not display the content after opening the shutter
2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
如何使用ConcurrentLinkedQueue做一个缓存队列
Why does invariant mode improve performance
Jerry's ad series MIDI function description [chapter]
How is the entered query SQL statement executed?