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

JavaScript中判断元素是否在可视区域内

JavaScript中判断元素是否在可视区域内

  • 1.有什么应用
  • 2.方法1-----使用offsetTop、scrollTop
  • 3.方法2-----getBoundingClientRect
  • 4.方法3-----Intersection Observer

1.有什么应用

  • 懒加载
  • 列表的无限滚动
  • 计算广告元素的曝光情况

2.方法1-----使用offsetTop、scrollTop

offsetTop、scrollTop属于三大家族的内容,详情见https://blog.csdn.net/fageaaa/article/details/145728760。

思路:元素的offsetTop-页面的scrollTop<=页面的高度

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>Title</title><style>.area1,.area3 {width: 100%;height: 1200px;background-color: aqua;}.box {width: 100%;height: 100px;background-color: red;}</style></head><body><div class="area1"></div><div class="area2 box"></div><div class="area3"></div><script>function isInViewPortOfOne(el) {// 下面是浏览器视口的高度的兼容性写法const viewPortHeight =window.innerHeight ||document.documentElement.clientHeight ||document.body.clientHeight;const offsetTop = el.offsetTop;const scrollTop = document.documentElement.scrollTop;const top = offsetTop - scrollTop;//注意这里只是个简略方式,没考虑el的边框return top <= viewPortHeight && top + el.clientHeight >= 0;}let box= document.querySelector(".box");window.addEventListener("scroll", function () {if (isInViewPortOfOne(box)) {console.log("box元素在可视区域内");} else {console.log("box元素不在可视区域内");}});</script></body>
</html>

3.方法2-----getBoundingClientRect

getBoundingClientRect函数返回值是一个 DOMRect对象,拥有left, top, right, bottom, x, y, width, 和 height属性。详情见https://blog.csdn.net/fageaaa/article/details/145728760。
思路:top大于等于0;left大于等于0;right小于等于viewWidthbottom小于等于viewHeight

function isInViewPort(element) {const viewWidth = window.innerWidth || document.documentElement.clientWidth;const viewHeight = window.innerHeight || document.documentElement.clientHeight;const {top,right,bottom,left,} = element.getBoundingClientRect();return (top >= 0 &&left >= 0 &&right <= viewWidth &&bottom <= viewHeight);
}

4.方法3-----Intersection Observer

前面两种方式需要一直监听,就算用了防抖节流也要一直监听(只不过性能会好一点)。所以Intersection Observer这个方法性能上会更好!!!

浏览器提供了一种异步观察目标元素与祖先元素(或顶级文档的视口)交叉状态变化的方法。IntersectionObserver详细用法见https://blog.csdn.net/fageaaa/article/details/145741778

<head><meta charset="UTF-8" /><title>Title</title><style>.area1,.area3 {width: 100%;height: 1200px;background-color: aqua;}.box {width: 100%;height: 100px;background-color: red;}</style></head><body><div class="area1"></div><div class="area2 box"></div><div class="area3"></div><script>let box = document.querySelector(".box");//它提供了一种异步观察目标元素与祖先元素(或顶级文档的视口)交叉状态变化的方法。// 这个API的出现主要是为了高效解决在网页开发中需要频繁判断元素是否进入“视口”(viewport)的问题。// 相比于传统的依赖scroll事件和getBoundingClientRect方法,Intersection Observer API在性能上有显著的优势let intersectionObserver = new IntersectionObserver(function (el) {if (el[0].intersectionRatio <= 0) {console.log("元素彻底离开可视区");} else {console.log("元素进入可视区");//进入可视区之后,资源已经下载好,就不需要再进行监视了intersectionObserver.unobserve(box);}});intersectionObserver.observe(box);</script></body>

http://www.mrgr.cn/news/91570.html

相关文章:

  • 机器学习实战(7):聚类算法——发现数据中的隐藏模式
  • deepseek-r1系列模型部署分别需要的最低硬件配置
  • frameworks 之 Activity添加View
  • 【愚公系列】《Python网络爬虫从入门到精通》022-Splash的爬虫应用
  • C++17 中的 std::uncaught_exceptions:异常处理的新利器
  • 数学推理中在推理规模化下检查假阳性解
  • Windows 图形显示驱动开发-IoMmu 模型
  • 关于 BK3633 上电时受串口 UART2 影响而无法启动的问题说明
  • 2025年人工智能与教育系统国际学术会议(ICAIES 2025)
  • 远程计算机无conda情况下配置python虚拟环境
  • 风铃摇晃的弧度与不安等长
  • 使用 Ansys HFSS 对植入式医疗设备进行无线电力传输和 SAR 仿真
  • 【动态路由】系统Web URL资源整合系列(后端技术实现)【apisix实现】
  • Jmeter断言、关联、录制脚本
  • Navicat16安装教程(附安装包)2025最新版详细图文安装教程
  • 【动态路由】系统Web URL资源整合系列(后端技术实现)【nodejs实现】
  • 小胡说技书博客分类(部分目录):服务治理、数据治理与安全治理对比表格
  • 【Linux】命令操作、打jar包、项目部署
  • 如何简单的去使用jconsloe 查看线程 (多线程编程篇1)
  • 某大型业务系统技术栈介绍【应对面试】