当前位置:网站首页>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)
}
})
}
},
}
边栏推荐
- 【LeetCode】17、电话号码的字母组合
- Configuration of DNS server of Huawei ENSP simulator
- Jerry added the process of turning off the touch module before turning it off [chapter]
- MP3是如何诞生的?
- Huawei ENSP simulator realizes communication security (switch)
- 应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
- MP3是如何诞生的?
- Redis 排查大 key 的3种方法,优化必备
- 每日一题-LeetCode1200-最小绝对差-数组-排序
- Drop down selection of Ehlib database records
猜你喜欢
Application practice | Shuhai supply chain construction of data center based on Apache Doris
Case sharing | integrated construction of data operation and maintenance in the financial industry
ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start
[C language] deep understanding of symbols
Shutter textfield example
开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏
解读创客教育中的各类智能化组织发展
一文掌握数仓中auto analyze的使用
Super detailed tutorial, an introduction to istio Architecture Principle and practical application
每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation
随机推荐
Master the use of auto analyze in data warehouse
redis RDB AOF
Huawei ENSP simulator configures ACL access control list
[weekly translation go] how to code in go series articles are online!!
GTEST from ignorance to skillful use (1) GTEST installation
QT—双缓冲绘图
Learning breakout 3 - about energy
WGCNA analysis basic tutorial summary
Jerry's ad series MIDI function description [chapter]
Difference between ApplicationContext and beanfactory (MS)
Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
Huawei ENSP simulator realizes communication security (switch)
超详细教程,一文入门Istio架构原理及实战应用
WGCNA分析基本教程总结
MP3是如何诞生的?
flink1.13 sql基础语法(一)DDL、DML
奋斗正当时,城链科技战略峰会广州站圆满召开
Go语言循环语句(第10课中3)
Le module minidom écrit et analyse XML
Jerry's ad series MIDI function description [chapter]