当前位置: 首页 > web >正文

`IntersectionObserver`延迟加载不在首屏的自动播放视频/图片/埋点/

IntersectionObserver 是非常实用的 浏览器 API,用来监听 元素是否进入/离开视口(viewport) 或某个容器。它让我们不用频繁写 scroll 事件,提高性能。

let observer = new IntersectionObserver(callback, options)

callback:当被观察元素进入或离开视口时触发,返回 entries(被观察的元素状态数组)。

options:配置对象:

  • root:观察的容器(默认是 viewport)。

  • rootMargin:扩展/收缩 root 的边距(类似 margin,支持 “10px 20px”)。

  • threshold:触发回调的阈值,可以是 0~1 的数或数组。

isIntersecting

类型:Boolean

含义:目标元素和根容器是否相交(哪怕相交 1 个像素,也算 true)。

常用来判断元素是否进入视口(或指定容器)。

callback

const observer = new IntersectionObserver(callback, options)function callback(entries, observer) {// entries: IntersectionObserverEntry[] // observer: IntersectionObserver 实例本身
}

entries:一个数组,里面是若干个 IntersectionObserverEntry 对象(每个被观察的元素对应一个)。

observer:当前的 IntersectionObserver 实例(很少直接用,但可以在里面调用 unobserve 或 disconnect)。

const box = document.querySelector('.box')const observer = new IntersectionObserver((entries, observer) => {entries.forEach(entry => {console.log('被观察元素:', entry.target)console.log('是否可见:', entry.isIntersecting)console.log('可见比例:', entry.intersectionRatio)console.log('目标矩形:', entry.boundingClientRect)console.log('可见区域:', entry.intersectionRect)console.log('根容器矩形:', entry.rootBounds)console.log('时间戳:', entry.time)// 可选:停止观察if (entry.isIntersecting) {observer.unobserve(entry.target)}})
}, { threshold: [0, 0.5, 1] })observer.observe(box)

场景举例

图片懒加载:用 entry.isIntersecting 判断是否进入视口。

曝光统计:用 entry.intersectionRatio >= 0.5 表示“至少一半可见”。

动画触发:用 entry.isIntersecting 为 true 时加 className 启动动画。

图片懒加载

<img data-src="big-image.jpg" alt="demo" width="600" height="400">
<script>
const img = document.querySelector('img');const observer = new IntersectionObserver((entries, observer) => {entries.forEach(entry => {if(entry.isIntersecting) {// 替换真实图片地址entry.target.src = entry.target.dataset.src;observer.unobserve(entry.target); // 加载后不再观察}});
});observer.observe(img);
</script>

无限滚动加载更多数据

const sentinel = document.querySelector('#sentinel'); // 页面底部的触发点const observer = new IntersectionObserver(entries => {if(entries[0].isIntersecting) {loadMoreData(); // 加载更多数据}
});observer.observe(sentinel);

元素曝光统计(广告/埋点)

const boxes = document.querySelectorAll('.ad');const observer = new IntersectionObserver(entries => {entries.forEach(entry => {if(entry.isIntersecting) {console.log('广告被看到:', entry.target);observer.unobserve(entry.target); // 只记录一次}});
}, { threshold: 0.5 }); // 一半区域可见时算曝光boxes.forEach(box => observer.observe(box));

使用场景总结

  • 图片/视频懒加载:只在进入视口时加载。

  • 无限滚动列表:像微博、瀑布流,滑到页面底部再加载。

  • 曝光监测/埋点:广告、统计用户是否真正看到元素。

  • 动画触发:某元素滚动到可见时触发渐显、滑入等动画。

  • 性能优化:替代 scroll + getBoundingClientRect() 的方案。

延迟加载不在首屏的自动播放视频

See the Pen 延迟加载不在首屏的自动播放视频
http://www.xdnf.cn/news/20511.html

相关文章:

  • Boost电路:稳态和小信号分析
  • Linux匿名管道和命名管道以及共享内存
  • C++并发编程指南 递归锁 介绍
  • Kimi K2-0905 256K 上下文 API 状态管理优化教程
  • 2.虚拟内存:分页、分段、页面置换算法
  • 分享一个基于Python+大数据的房地产一手房成交数据关联分析与可视化系统,基于机器学习的深圳房产价格走势分析与预测系统
  • Embedding上限在哪里?- On the Theoretical Limitations of Embedding-Based Retrieval
  • AI产品经理面试宝典第86天:提示词设计核心原则与面试应答策略
  • 《sklearn机器学习——聚类性能指标》Calinski-Harabaz 指数
  • Wisdom SSH 是一款搭载强大 AI 助手的工具,能显著简化服务器配置管理流程。
  • SSH服务远程安全登录
  • Linux系统shell脚本(四)
  • CodeSandbox Desktop:零配置项目启动工具,实现项目环境隔离与Github无缝同步
  • AI大模型应用研发工程师面试知识准备目录
  • 苍穹外卖优化-续
  • Java包装类型
  • Git 长命令变短:一键设置别名
  • Linux以太网模块
  • 【嵌入式】【科普】AUTOSAR学习路径
  • 《无畏契约》游戏报错“缺少DirectX”?5种解决方案(附DirectX修复工具)
  • 基于单片机智能行李箱设计
  • 云手机运行流畅,秒开不卡顿
  • 无拥塞网络的辩证
  • 24.线程概念和控制(一)
  • 贪心算法应用:数字孪生同步问题详解
  • B.50.10.10-微服务与电商应用
  • 关于退耦电容
  • 【LeetCode热题100道笔记】将有序数组转换为二叉搜索树
  • 3分钟快速入门WebSocket
  • Scikit-learn Python机器学习 - 特征降维 压缩数据 - 特征提取 - 主成分分析 (PCA)