<script>
let intersectionObserver = new IntersectionObserver(function(entries) {
if (entries[0].isIntersecting) {
console.log('进入可视区域');
} else {
console.log('移出可视区域');
}
});
intersectionObserver.observe(document.querySelector('#content'));
</script>
IntersectionObserver-监听元素是否进入了设备的可视区域之内
最新推荐文章于 2025-02-20 00:22:41 发布
这段代码展示了如何使用Intersection Observer API来监听页面上#content元素的可视状态。当元素进入或离开视口时,将在控制台打印相应的消息。这是一种优化网页性能,仅在元素可见时执行某些操作的方法。
1996

被折叠的 条评论
为什么被折叠?



