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

韩都衣舍的网站建设百度产品大全首页

韩都衣舍的网站建设,百度产品大全首页,iphone网站,微信卖货小程序JavaScript中判断元素是否在可视区域内 1.有什么应用2.方法1-----使用offsetTop、scrollTop3.方法2-----getBoundingClientRect4.方法3-----Intersection Observer 1.有什么应用 懒加载列表的无限滚动计算广告元素的曝光情况 2.方法1-----使用offsetTop、scrollTop offsetTo…

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.dtcms.com/wzjs/8623.html

相关文章:

  • 女同wordpressseo外贸推广
  • 武汉哪个网站建设最好合肥seo推广公司
  • 网站存在风险什么意思app推广拉新平台
  • 免费网络空间黄山网站seo
  • 网站建设制作包括哪些国内可访问的海外网站和应用
  • 上海网站建设 推荐站霸网络网站推广专家
  • 做哪类网站比较赚钱怎么建立网站?
  • 自己做网站要买什么武汉大学人民医院怎么样
  • 商城网站建设模板百度云网盘搜索引擎入口
  • 品牌建设实施纲要天津seo推广
  • 库尔勒网站商城建设优化神马网站关键词排名价格
  • 网站开发方案案例百度识图官网
  • wordpress 主题2周前网站seo是什么
  • 中企动力 网站报价网络营销推广渠道有哪些
  • 企业手机网站建设价位竞价推广公司
  • 信息聚合网站怎么做怎么优化推广自己的网站
  • 做网站运营有前景吗最近一周新闻大事摘抄2022年
  • 做网站的创业计划书网络推广服务协议
  • 手机网站的必要性营销网站定制
  • 自己做的网站找不到了win7优化设置
  • 网站做302跳转的意义做推广哪个平台效果好
  • ppt制作软件模板网站百度自然搜索排名优化
  • 做网站投入免费网站seo排名优化
  • 莆田市商城网站建设我想做电商怎么加入
  • 荣成网站建设搜索引擎网站排名优化方案
  • 网站的在线支付怎么做个人推广网站
  • 做搜索关键词任务网站baidu百度首页
  • 简单做图网站好的营销网站
  • html的制作网站的优点铜陵seo
  • 网站开发项目商业计划书湖南关键词优化快速