当前位置:网站首页>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)
}
})
}
},
}
边栏推荐
- 【C语言】符号的深度理解
- [leetcode] 17. Letter combination of telephone number
- Go language loop statement (3 in Lesson 10)
- Rotary transformer string judgment
- WGCNA分析基本教程总结
- Three or two things about the actual combat of OMS system
- The video sound of station B is very low - solution
- 类方法和类变量的使用
- SolidWorks工程图添加材料明细表的操作
- 面试官:说说XSS攻击是什么?
猜你喜欢

QT—双缓冲绘图

输入的查询SQL语句,是如何执行的?

解析steam教育中蕴含的众创空间

WGCNA分析基本教程总结

Huawei ENSP simulator realizes communication security (switch)
![[optimtool.unconstrained] unconstrained optimization toolbox](/img/ef/65379499df205c068ee9bc9df797ac.png)
[optimtool.unconstrained] unconstrained optimization toolbox

TCP三次握手,四次挥手,你真的了解吗?

Master the use of auto analyze in data warehouse

CAD中能显示打印不显示
![Jerry's ad series MIDI function description [chapter]](/img/28/e0f9b41db597ff3288af431c677001.png)
Jerry's ad series MIDI function description [chapter]
随机推荐
Jerry's ad series MIDI function description [chapter]
Three or two things about the actual combat of OMS system
Redis pipeline
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
minidom 模塊寫入和解析 XML
Solve the problem of data disorder caused by slow asynchronous interface
redis缓存
Shutter WebView example
Flink1.13 SQL basic syntax (I) DDL, DML
gtest从一无所知到熟练使用(3)什么是test suite和test case
EhLib 数据库记录的下拉选择
股票开户佣金最低多少,炒股开户佣金最低网上开户安全吗
Kubeadm初始化报错:[ERROR CRI]: container runtime is not running
Jerry's ad series MIDI function description [chapter]
2021 CCPC 哈尔滨 B. Magical Subsequence(思维题)
Redis bloom filter
redis管道
redis RDB AOF
【活动早知道】LiveVideoStack近期活动一览
[weekly translation go] how to code in go series articles are online!!