实现方案
主要有三种:
- 监听Scroll方案
- Intersection Observer方案
- 轮询
在开始之前,假设自己有一些工具方法
1 | // 判断dom元素是否在视口中 |
一、监听Scroll方案
1 | <!-- html模板 --> |
1 | const nodes = document.querySelectorAll('.scroll-list-item'); |
二、轮询(定时器)
因为方法一中,dom结构不同,并且方法不够通用,所以思考新的方式。
使用setInterval,则不用考虑元素是怎么出现的,只要出现在了屏幕中,则定时器会自动发现需要曝光的内容。
1 | <div data-expose="商品1">...</div> |
1 | const observeTimer = setInterval(() => { |
二、Intersection Observer方案
IntersectionObserver接口提供了一种异步观察目标元素与其祖先元素或顶级文档视窗(viewport)交叉状态的方法。
1 | <!-- html模板 --> |
1 | sendReport() { |
但是w3c官方提供了polyfill:https://github.com/w3c/IntersectionObserver/tree/main/polyfill (每个提案 到 Working Draft阶段通常会提供1-2个polyfill). 然后兼容性就可以说是起飞了。
参考:
https://qastack.cn/programming/123999/how-can-i-tell-if-a-dom-element-is-visible-in-the-current-viewport
https://routinepanic.com/questions/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport
https://juejin.cn/post/6844903886013071373#heading-2
https://developer.mozilla.org/zh-CN/docs/Web/API/Element/getBoundingClientRect
https://shuliqi.github.io/2020/03/26/%E4%BD%BF%E7%94%A8IntersectionObserver%E6%8F%90%E9%AB%98%E6%80%A7%E8%83%BD/#%E6%80%BB%E7%BB%93