new IntersectionObserver 使用笔记

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)
            }
          })
        }
      },
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值