概述
通过设置 `rootMargin` 扩展观察区域可提前触发懒加载,在滚动接近时预取资源;通过 `thresholds` 定义多个交叉比例实现更精细的曝光采集与动效控制。
示例
```js
const io = new IntersectionObserver((entries) => {
for (const e of entries) {
if (e.isIntersecting) { loadResource(e.target) }
}
}, { rootMargin: '200px 0px', threshold: [0, 0.25, 0.5, 0.75, 1] })
document.querySelectorAll('.lazy').forEach(el => io.observe(el))
```
工程建议
- 提前加载:根据资源体积与网络条件设置 `rootMargin`;避免过度提前导致带宽竞争。
- 曝光精度:按业务需求选择阈值;记录曝光与停留时间用于分析。
- 兼容:旧浏览器回退到滚动监听与节流;保持体验一致。
参考与验证
- MDN IntersectionObserver 文档:https://developer.mozilla.org/docs/Web/API/IntersectionObserver
- web.dev 懒加载与曝光指南:https://web.dev/articles/lazy-loading

发表评论 取消回复