当前位置:网站首页>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)
}
})
}
},
}
边栏推荐
- 【活动早知道】LiveVideoStack近期活动一览
- Numpy vstack and column_ stack
- EhLib 数据库记录的下拉选择
- Flutter在 release版本,打开后随机白屏不显示内容
- 解决异步接口慢导致的数据错乱问题
- [optimtool.unconstrained] unconstrained optimization toolbox
- Huawei ENSP simulator enables devices of multiple routers to access each other
- GTEST from ignorance to skillful use (1) GTEST installation
- Huawei ENSP simulator realizes communication security (switch)
- [buuctf.reverse] 151_ [FlareOn6]DnsChess
猜你喜欢
![Jerry added the process of turning off the touch module before turning it off [chapter]](/img/28/5e4eb39243a0c973d0b90f76571f9b.png)
Jerry added the process of turning off the touch module before turning it off [chapter]

Daily question -leetcode1200- minimum absolute difference - array - sort
![[early knowledge of activities] list of recent activities of livevideostack](/img/14/d2cdae45a18a5bba7ee1ffab903af2.jpg)
[early knowledge of activities] list of recent activities of livevideostack

Maya lamp modeling

Huawei ENSP simulator configures ACL access control list

【C语言】符号的深度理解

改善机器视觉系统的方法

QT—双缓冲绘图

【活动早知道】LiveVideoStack近期活动一览

Methods of improving machine vision system
随机推荐
2021 CCPC Harbin B. magical subsequence (thinking question)
如何借助自动化工具落地DevOps
How to implement Devops with automatic tools
Jerry's ad series MIDI function description [chapter]
如何使用ConcurrentLinkedQueue做一个缓存队列
面试官:说说XSS攻击是什么?
Go语言循环语句(第10课中3)
Acwing 2022 daily question
Golang interview finishing three resumes how to write
[buuctf.reverse] 151_ [FlareOn6]DnsChess
Jerry's ad series MIDI function description [chapter]
QT—双缓冲绘图
redis RDB AOF
案例分享|金融业数据运营运维一体化建设
CAD中能显示打印不显示
[wechat applet] collaborative work and release
Compréhension approfondie du symbole [langue C]
Hash table
Open3d surface normal vector calculation
How was MP3 born?