用 IntersectionObserver 替代滚动监听:更准、更省、更简单
页面里做懒加载、无限滚动、元素曝光埋点、吸顶导航切换时,很多项目第一反应还是给 window 挂一个 scroll 事件监听,在回调里用 getBoundingClientRect() 或者 offsetTop 判断元素是否进入视口。这个做法能跑,但代价不小:滚动事件触发频率极高,哪怕加了节流,每次回调仍然可能触发同步布局计算,导致样式重算和绘制排队;如果判断逻辑里读取了 offsetHeight、scrollTop 这类属性,还会造成强制同步布局。更麻烦的是,当页面里有多个滚动容器,或者元素在 iframe、嵌套 flex 布局中时,手写判断的边界条件很容易写错。IntersectionObserver 是浏览器原生提供的异步观察方案,它把“元素是否与视口或某个祖先容器相交”这件事交给浏览器内部去算,回调只在相交状态跨过阈值时触发,既避免了高频计算,也天然支持自定义 root、rootMargin 和 threshold。下面结合几个真实场景,看看怎么把它用得顺手。 ## 一个最常见的懒加载 图片懒加载是 IntersectionObserver 最典型的用法。过去监听 scroll 的写法大致是这样: ```js const imgs = document.querySelectorAll('img[data-src]') function lazyLoad() { const viewportHeight = window.innerHeight imgs.forEach(img => { const rect = img.getBoundingClientRect() if (rect.top 0) { img.src = img.dataset.src } }) } window.addEventListener('scroll', throttle(lazyLoad, 200)) lazyLoad() ``` 换成 IntersectionObserver 后,代码量减少,滚动时也没有额外计算: ```js const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { const img = entry.target img.src = img.dataset.src observer.unobserve(img) } }) }, { rootMargin: '0px 0px 200px 0px' }) document.querySelectorAll('img[data-src]').forEach(img => { observer.observe(img) }) ``` rootMargin 设为底部 200px,意味着图片距离视口还有 200px 时就开始加载,用户滚到的时候基本已经加载完成。这个预加载距离用 scroll 方案也能做,但需要自己算,而 rootMargin 的语法和 CSS margin 一致,很好理解。 加载完成后调用 unobserve 是必要的。图片一旦加载就不需要再观察,继续观察只会让浏览器保留无用的观察记录。如果元素会被移除,记得在移除前 unobserve,否则 observer 会持有对已移除元素的引用。 ## threshold 不只是 0 和 1 threshold 表示目标元素可见比例达到多少时触发回调,可以是单个数字,也可以是数组。曝光埋点常用 threshold: 0,只要露出一点就算曝光;但有些业务要求“元素至少一半可见”才算有效曝光,这时写 0.5: ```js const exposureObserver = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.intersectionRatio >= 0.5) { reportExposure(entry.target.dataset.id) exposureObserver.unobserve(entry.target) } }) }, { threshold: 0.5 }) ``` 如果传数组,比如 [0, 0.25, 0.5, 0.75, 1],回调会在每个阈值被跨过时触发。entry.intersectionRatio 是当前可见比例,entry.isIntersecting 等价于 ratio 大于 0。用数组做进度条或者视频自动播放时比较有用,比如视频可见超过 75% 才自动播放: ```js const videoObserver = new IntersectionObserver((entries) => { entries.forEach(entry => { const video = entry.target if (entry.intersectionRatio >= 0.75) { video.play().catch(() => {}) } else { video.pause() } }) }, { threshold: [0, 0.75] }) ``` 这里 threshold 用 [0, 0.75],是因为从不可见变为可见、以及从可见变为不可见都需要回调,只写 0.75 的话,元素完全离开视口时不会触发暂停。 ## 无限滚动里的 root 和 rootMargin 无限滚动常见的实现是监听页面底部的一个 sentinel 元素,它进入视口就去请求下一页。如果滚动容器不是 window,而是某个 overflow: auto 的 div,就需要把 root 指向这个容器: ```html ``` ```js const container = document.getElementById('listContainer') const sentinel = document.getElementById('sentinel') const loadMoreObserver = new IntersectionObserver((entries) => { if (entries[0].isIntersecting) { loadNextPage() } }, { root: container, rootMargin: '0px 0px 300px 0px' }) loadMoreObserver.observe(sentinel) ``` root 必须是 sentinel 的祖先元素,否则观察不会生效。rootMargin 在这里同样用于提前触发,用户还没滚到底就开始请求下一页,体验会连贯很多。 要注意的是,如果 sentinel 一开始就在视口内,回调会立即触发一次,这可能导致页面加载后连续请求多页。实际项目里通常会加一个 loading 标志位,请求完成前不再触发: ```js let loading = false async function loadNextPage() { if (loading) return loading = true try { const data = await fetchList() renderList(data) } finally { loading = false } } ``` ## 吸顶导航切换用 root 和 rootMargin 控制 长页面里,某个区块滚到顶部时导航高亮切换,也可以用 IntersectionObserver。假设导航栏高度 60px,希望区块顶部进入导航栏下方时切换高亮,可以这样: ```js const sections = document.querySelectorAll('section[data-nav]') const navLinks = document.querySelectorAll('.nav-link') const navObserver = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { const id = entry.target.dataset.nav navLinks.forEach(link => { link.classList.toggle('active', link.dataset.target === id) }) } }) }, { rootMargin: '-60px 0px -70% 0px' }) sections.forEach(section => navObserver.observe(section)) ``` rootMargin 的四个值分别对应上、右、下、左。上边距 -60px 把视口顶部往下压了 60px,避开了固定导航;下边距 -70% 把视口底部往上收,这样只有区块顶部附近进入“有效区域”时才触发,避免多个区块同时判定为相交。 这种写法比 scroll + scrollTop 计算更稳定,因为不需要在回调里读取布局属性,也不会因为页面内容动态变化而导致计算失效。 ## 几个容易踩的坑 IntersectionObserver 的回调是异步的,不保证在下一帧之前执行。如果依赖它来同步更新 UI,可能会有一帧的延迟。对于吸顶导航这类切换,通常可以接受;但如果需要立即响应,还是要结合其他方式。 观察的元素如果 display: none,不会触发回调,直到它重新显示。这个特性可以用来做条件观察,但也可能让开发者误以为 observer 失效。 root 和 target 之间如果隔着 shadow DOM,观察不会跨 shadow 边界工作,需要把 root 设置为 shadow root。 另外,IntersectionObserver 不提供元素具体位置,只提供相交比例和边界信息。需要精确坐标时,仍然要配合 getBoundingClientRect,但这时候调用频率已经因为阈值触发而大幅降低了。 兼容性方面,现代浏览器都支持,IE 不支持。如果项目还要兼容 IE,可以用 polyfill,它会退化为 scroll 监听,但接口保持一致,日后移除 polyfill 不需要改业务代码。 ## 什么时候不该用它 需要实时知道滚动位置的场景,比如视差滚动、滚动进度条,IntersectionObserver 就不合适,因为它只在阈值跨越时触发,拿不到连续的位置数据。这类场景还是得用 scroll 事件,但可以配合 requestAnimationFrame 或者原生的 scroll-driven animations 来减少开销。 简单来说,判断“在不在视口里”“露出多少”用 IntersectionObserver,跟踪“滚了多远”用 scroll。把这两类需求分开,代码会清晰很多,性能也更好。 推荐查看天气:http://www.362tq.com/minfeng/zuotian.html 推荐查看天气:http://www.362tq.com/minfeng/7tian.html 推荐查看天气:http://www.362tq.com/minfeng/10tian.html 推荐查看天气:http://www.362tq.com/minfeng/15tian.html 推荐查看天气:http://www.362tq.com/minfeng/40tian.html 推荐查看天气:http://www.362tq.com/yili/ 推荐查看天气:http://www.362tq.com/yili/zuotian.html 推荐查看天气:http://www.362tq.com/yili/7tian.html 推荐查看天气:http://www.362tq.com/yili/10tian.html 推荐查看天气:http://www.362tq.com/yili/15tian.html 推荐查看天气:http://www.362tq.com/yili/40tian.html 推荐查看天气:http://www.362tq.com/yining/ 推荐查看天气:http://www.362tq.com/yining/zuotian.html 推荐查看天气:http://www.362tq.com/yining/7tian.html 推荐查看天气:http://www.362tq.com/yining/10tian.html 推荐查看天气:http://www.362tq.com/yining/15tian.html 推荐查看天气:http://www.362tq.com/yining/40tian.html 推荐查看天气:http://www.362tq.com/kuitun/ 推荐查看天气:http://www.362tq.com/kuitun/zuotian.html 推荐查看天气:http://www.362tq.com/kuitun/7tian.html 推荐查看天气:http://www.362tq.com/kuitun/10tian.html 推荐查看天气:http://www.362tq.com/kuitun/15tian.html 推荐查看天气:http://www.362tq.com/kuitun/40tian.html 推荐查看天气:http://www.362tq.com/yiningxian/ 推荐查看天气:http://www.362tq.com/yiningxian/zuotian.html 推荐查看天气:http://www.362tq.com/yiningxian/7tian.html 推荐查看天气:http://www.362tq.com/yiningxian/10tian.html 推荐查看天气:http://www.362tq.com/yiningxian/15tian.html 推荐查看天气:http://www.362tq.com/yiningxian/40tian.html 推荐查看天气:http://www.362tq.com/chabuchaer/ 推荐查看天气:http://www.362tq.com/chabuchaer/zuotian.html 推荐查看天气:http://www.362tq.com/chabuchaer/7tian.html 推荐查看天气:http://www.362tq.com/chabuchaer/10tian.html 推荐查看天气:http://www.362tq.com/chabuchaer/15tian.html 推荐查看天气:http://www.362tq.com/chabuchaer/40tian.html 推荐查看天气:http://www.362tq.com/huocheng/ 推荐查看天气:http://www.362tq.com/huocheng/zuotian.html 推荐查看天气:http://www.362tq.com/huocheng/7tian.html 推荐查看天气:http://www.362tq.com/huocheng/10tian.html 推荐查看天气:http://www.362tq.com/huocheng/15tian.html 推荐查看天气:http://www.362tq.com/huocheng/40tian.html 推荐查看天气:http://www.362tq.com/gongliu/ 推荐查看天气:http://www.362tq.com/gongliu/zuotian.html 推荐查看天气:http://www.362tq.com/gongliu/7tian.html 推荐查看天气:http://www.362tq.com/gongliu/10tian.html 推荐查看天气:http://www.362tq.com/gongliu/15tian.html 推荐查看天气:http://www.362tq.com/gongliu/40tian.html 推荐查看天气:http://www.362tq.com/xinyuan/ 推荐查看天气:http://www.362tq.com/xinyuan/zuotian.html 推荐查看天气:http://www.362tq.com/xinyuan/7tian.html 推荐查看天气:http://www.362tq.com/xinyuan/10tian.html 推荐查看天气:http://www.362tq.com/xinyuan/15tian.html 推荐查看天气:http://www.362tq.com/xinyuan/40tian.html 推荐查看天气:http://www.362tq.com/zhaosu/ 推荐查看天气:http://www.362tq.com/zhaosu/zuotian.html 推荐查看天气:http://www.362tq.com/zhaosu/7tian.html 推荐查看天气:http://www.362tq.com/zhaosu/10tian.html 推荐查看天气:http://www.362tq.com/zhaosu/15tian.html 推荐查看天气:http://www.362tq.com/zhaosu/40tian.html 推荐查看天气:http://www.362tq.com/tekesi/ 推荐查看天气:http://www.362tq.com/tekesi/zuotian.html 推荐查看天气:http://www.362tq.com/tekesi/7tian.html 推荐查看天气:http://www.362tq.com/tekesi/10tian.html 推荐查看天气:http://www.362tq.com/tekesi/15tian.html 推荐查看天气:http://www.362tq.com/tekesi/40tian.html 推荐查看天气:http://www.362tq.com/nileke/ 推荐查看天气:http://www.362tq.com/nileke/zuotian.html 推荐查看天气:http://www.362tq.com/nileke/7tian.html 推荐查看天气:http://www.362tq.com/nileke/10tian.html 推荐查看天气:http://www.362tq.com/nileke/15tian.html 推荐查看天气:http://www.362tq.com/nileke/40tian.html 推荐查看天气:http://www.362tq.com/tachengdiqu/ 推荐查看天气:http://www.362tq.com/tachengdiqu/zuotian.html 推荐查看天气:http://www.362tq.com/tachengdiqu/7tian.html 推荐查看天气:http://www.362tq.com/tachengdiqu/10tian.html 推荐查看天气:http://www.362tq.com/tachengdiqu/15tian.html 推荐查看天气:http://www.362tq.com/tachengdiqu/40tian.html 推荐查看天气:http://www.362tq.com/wusu/ 推荐查看天气:http://www.362tq.com/wusu/zuotian.html 推荐查看天气:http://www.362tq.com/wusu/7tian.html 推荐查看天气:http://www.362tq.com/wusu/10tian.html 推荐查看天气:http://www.362tq.com/wusu/15tian.html 推荐查看天气:http://www.362tq.com/wusu/40tian.html 推荐查看天气:http://www.362tq.com/emin/ 推荐查看天气:http://www.362tq.com/emin/zuotian.html 推荐查看天气:http://www.362tq.com/emin/7tian.html 推荐查看天气:http://www.362tq.com/emin/10tian.html 推荐查看天气:http://www.362tq.com/emin/15tian.html 推荐查看天气:http://www.362tq.com/emin/40tian.html 推荐查看天气:http://www.362tq.com/shawan/ 推荐查看天气:http://www.362tq.com/shawan/zuotian.html 推荐查看天气:http://www.362tq.com/shawan/7tian.html 推荐查看天气:http://www.362tq.com/shawan/10tian.html 推荐查看天气:http://www.362tq.com/shawan/15tian.html 推荐查看天气:http://www.362tq.com/shawan/40tian.html 推荐查看天气:http://www.362tq.com/tuoli/ 推荐查看天气:http://www.362tq.com/tuoli/zuotian.html 推荐查看天气:http://www.362tq.com/tuoli/7tian.html 推荐查看天气:http://www.362tq.com/tuoli/10tian.html 推荐查看天气:http://www.362tq.com/tuoli/15tian.html 推荐查看天气:http://www.362tq.com/tuoli/40tian.html 推荐查看天气:http://www.362tq.com/yumin/ 推荐查看天气:http://www.362tq.com/yumin/zuotian.html 推荐查看天气:http://www.362tq.com/yumin/7tian.html 推荐查看天气:http://www.362tq.com/yumin/10tian.html 推荐查看天气:http://www.362tq.com/yumin/15tian.html 推荐查看天气:http://www.362tq.com/yumin/40tian.html 推荐查看天气:http://www.362tq.com/hebukesaierxian/ 推荐查看天气:http://www.362tq.com/hebukesaierxian/zuotian.html 推荐查看天气:http://www.362tq.com/hebukesaierxian/7tian.html 推荐查看天气:http://www.362tq.com/hebukesaierxian/10tian.html 推荐查看天气:http://www.362tq.com/hebukesaierxian/15tian.html 推荐查看天气:http://www.362tq.com/hebukesaierxian/40tian.html 推荐查看天气:http://www.362tq.com/aletai/ 推荐查看天气:http://www.362tq.com/aletai/zuotian.html 推荐查看天气:http://www.362tq.com/aletai/7tian.html 推荐查看天气:http://www.362tq.com/aletai/10tian.html 推荐查看天气:http://www.362tq.com/aletai/15tian.html 推荐查看天气:http://www.362tq.com/aletai/40tian.html 推荐查看天气:http://www.362tq.com/buerjin/ 推荐查看天气:http://www.362tq.com/buerjin/zuotian.html 推荐查看天气:http://www.362tq.com/buerjin/7tian.html 推荐查看天气:http://www.362tq.com/buerjin/10tian.html 推荐查看天气:http://www.362tq.com/buerjin/15tian.html 推荐查看天气:http://www.362tq.com/buerjin/40tian.html 推荐查看天气:http://www.362tq.com/fuyun/ 推荐查看天气:http://www.362tq.com/fuyun/zuotian.html 推荐查看天气:http://www.362tq.com/fuyun/7tian.html 推荐查看天气:http://www.362tq.com/fuyun/10tian.html 推荐查看天气:http://www.362tq.com/fuyun/15tian.html 推荐查看天气:http://www.362tq.com/fuyun/40tian.html 推荐查看天气:http://www.362tq.com/fuhai/ 推荐查看天气:http://www.362tq.com/fuhai/zuotian.html 推荐查看天气:http://www.362tq.com/fuhai/7tian.html 推荐查看天气:http://www.362tq.com/fuhai/10tian.html 推荐查看天气:http://www.362tq.com/fuhai/15tian.html 推荐查看天气:http://www.362tq.com/fuhai/40tian.html 推荐查看天气:http://www.362tq.com/habahe/ 推荐查看天气:http://www.362tq.com/habahe/zuotian.html 推荐查看天气:http://www.362tq.com/habahe/7tian.html 推荐查看天气:http://www.362tq.com/habahe/10tian.html 推荐查看天气:http://www.362tq.com/habahe/15tian.html 推荐查看天气:http://www.362tq.com/habahe/40tian.html 推荐查看天气:http://www.362tq.com/qinghe/ 推荐查看天气:http://www.362tq.com/qinghe/zuotian.html 推荐查看天气:http://www.362tq.com/qinghe/7tian.html 推荐查看天气:http://www.362tq.com/qinghe/10tian.html 推荐查看天气:http://www.362tq.com/qinghe/15tian.html 推荐查看天气:http://www.362tq.com/qinghe/40tian.html 推荐查看天气:http://www.362tq.com/jimunai/ 推荐查看天气:http://www.362tq.com/jimunai/zuotian.html 推荐查看天气:http://www.362tq.com/jimunai/7tian.html 推荐查看天气:http://www.362tq.com/jimunai/10tian.html 推荐查看天气:http://www.362tq.com/jimunai/15tian.html 推荐查看天气:http://www.362tq.com/jimunai/40tian.html 推荐查看天气:http://www.362tq.com/xianggang/ 推荐查看天气:http://www.362tq.com/xianggang/zuotian.html 推荐查看天气:http://www.362tq.com/xianggang/7tian.html 推荐查看天气:http://www.362tq.com/xianggang/10tian.html 推荐查看天气:http://www.362tq.com/xianggang/15tian.html 推荐查看天气:http://www.362tq.com/xianggang/40tian.html 推荐查看天气:http://www.362tq.com/aomen/ 推荐查看天气:http://www.362tq.com/aomen/zuotian.html 推荐查看天气:http://www.362tq.com/aomen/7tian.html 推荐查看天气:http://www.362tq.com/aomen/10tian.html 推荐查看天气:http://www.362tq.com/aomen/15tian.html 推荐查看天气:http://www.362tq.com/aomen/40tian.html 推荐查看天气:http://www.362tq.com/shihezi/ 推荐查看天气:http://www.362tq.com/shihezi/zuotian.html 推荐查看天气:http://www.362tq.com/shihezi/7tian.html 推荐查看天气:http://www.362tq.com/shihezi/10tian.html 推荐查看天气:http://www.362tq.com/shihezi/15tian.html 推荐查看天气:http://www.362tq.com/shihezi/40tian.html 推荐查看天气:http://www.362tq.com/alaer/ 推荐查看天气:http://www.362tq.com/alaer/zuotian.html 推荐查看天气:http://www.362tq.com/alaer/7tian.html 推荐查看天气:http://www.362tq.com/alaer/10tian.html 推荐查看天气:http://www.362tq.com/alaer/15tian.html 推荐查看天气:http://www.362tq.com/alaer/40tian.html 推荐查看天气:http://www.362tq.com/tumushuke/ 推荐查看天气:http://www.362tq.com/tumushuke/zuotian.html 推荐查看天气:http://www.362tq.com/tumushuke/7tian.html 推荐查看天气:http://www.362tq.com/tumushuke/10tian.html 推荐查看天气:http://www.362tq.com/tumushuke/15tian.html 推荐查看天气:http://www.362tq.com/tumushuke/40tian.html 推荐查看天气:http://www.362tq.com/wuzhishan/ 推荐查看天气:http://www.362tq.com/wuzhishan/zuotian.html 推荐查看天气:http://www.362tq.com/wuzhishan/7tian.html 推荐查看天气:http://www.362tq.com/wuzhishan/10tian.html 推荐查看天气:http://www.362tq.com/wuzhishan/15tian.html 推荐查看天气:http://www.362tq.com/wuzhishan/40tian.html 推荐查看天气:http://www.362tq.com/qionghai/ 推荐查看天气:http://www.362tq.com/qionghai/zuotian.html 推荐查看天气:http://www.362tq.com/qionghai/7tian.html 推荐查看天气:http://www.362tq.com/qionghai/10tian.html 推荐查看天气:http://www.362tq.com/qionghai/15tian.html 推荐查看天气:http://www.362tq.com/qionghai/40tian.html 推荐查看天气:http://www.362tq.com/danzhou/ 推荐查看天气:http://www.362tq.com/danzhou/zuotian.html 推荐查看天气:http://www.362tq.com/danzhou/7tian.html