IntersectionObserver的用法

IntersectionObserver

IntersectionObserver 是 JavaScript 中用于监听元素与其祖先元素或视口(viewport)交叉状态的 API,常用于实现懒加载无限滚动广告曝光统计等功能。以下是其核心概念和用法:

核心概念

  • 交叉观察 :检测目标元素与根元素(默认为视口)的交集变化。
  • 阈值(Threshold) :交集比例达到多少时触发回调(如 0.5 表示元素显示一半时触发)。
  • 异步触发:由浏览器优化后异步执行,不会阻塞主线程。
js 复制代码
// 创建观察者实例
const observer = new IntersectionObserver(
  (entries, observer) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        // 元素进入视口时的操作
        console.log(`${entry.target.id} 进入视口`);
        observer.unobserve(entry.target); // 停止观察
      }
    });
  },
  {
    root: null, // 默认为视口
    rootMargin: '0px', // 根元素的边距(如 `'10px 0px'`),可扩大或缩小监听范围。
    threshold: 0.1 // 元素显示 10% 时触发回调
  }
);

// 观察目标元素
const target = document.getElementById('target');
observer.observe(target);

回调参数(entries 每个 entry 对象包含以下属性:

  • isIntersecting:布尔值,表示元素是否正在交叉。
  • intersectionRatio:交集比例(0~1)。
  • target:目标元素。
  • boundingClientRect:目标元素的位置信息。
  • intersectionRect:交集区域的位置信息。

常见应用场景

1. 图片懒加载

js 复制代码
const images = document.querySelectorAll('img[data-src]');

const imgObserver = new IntersectionObserver(
  (entries, observer) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        const img = entry.target;
        img.src = img.dataset.src; // 加载真实图片
        img.classList.add('loaded');
        observer.unobserve(img);
      }
    });
  },
  { threshold: 0.1 }
);

images.forEach(img => imgObserver.observe(img));

2. 无限滚动

js 复制代码
const loadMoreObserver = new IntersectionObserver(
  (entries) => {
    if (entries[0].isIntersecting) {
      loadMoreData(); // 加载更多数据
    }
  },
  { rootMargin: '100px' } // 提前 100px 触发加载
);

loadMoreObserver.observe(document.getElementById('load-more-trigger'));

3. 元素曝光统计

js 复制代码
const adObserver = new IntersectionObserver(
  (entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting && entry.intersectionRatio >= 0.5) {
        trackAdExposure(entry.target.id); // 统计广告曝光
        observer.unobserve(entry.target);
      }
    });
  },
  { threshold: 0.5 } // 至少显示 50% 时才统计
);

优点

  • 性能优化 :由浏览器原生实现,避免频繁的 scrollresize 事件监听。
  • 简化开发:无需手动计算元素位置,逻辑更清晰。
  • 异步执行:不阻塞主线程,提升页面响应速度。

缺点

  • 兼容性 :IE 不支持,需使用 polyfill(如 intersection-observer)。
  • 精度限制:无法精确控制像素级别的交叉检测。
相关推荐
xiaofeichaichai19 小时前
Webpack
前端·webpack·node.js
问心无愧051320 小时前
ctf show web入门111
android·前端·笔记
唐某人丶20 小时前
模型越来越强,我们还需要 Agent 工程吗?—— 从价值重估到 Harness 实践
前端·agent·ai编程
智码看视界20 小时前
现代Web开发基础:全栈工程师的起航点
前端·后端·c5全栈
JS菌20 小时前
手写一个 AI Agent 全栈项目:从沙箱执行到子智能体的完整实现
前端·人工智能·后端
excel21 小时前
HLS TS 文件损坏的元凶:Git 提交与拉取
前端
Aphasia3111 天前
https连接传输流程
前端·面试
徐小夕1 天前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
threelab1 天前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器