控制元素到达可视区域内触发动效

在可视化区域内出发动效,首先需要先判断是否在可视化区域内,下文是判断是否在可视化区域的几种方法。
(在学习判断元素是否在可视区域时,我们首先的了解一些元素的位置值和大小值。
大部分属性都是对应的,所以下面都只写一个。
1、clientWidth:元素内容区宽度加上左右内边距宽度,即clientWidth = content + padding
2、offsetTop,元素的上外边框至包含元素的上内边框之间的像素距离。 // 元素的偏移量不会随着滚动条的滚动而发生改变。并且是相对于定位父元素的位置计算的。如果没有定位的父元素就获取的是到窗口的距离,从元素的外边框计算到父元素的内边框
3、document.documentElement.scrollHeight: 获取浏览器窗口的总高度 。包括滚动条的隐藏高度。,如果没有滚动条,则他就等于document.documentElement.clientWidth。
4、document.documentElement.clientWidth:获取视口宽度。就是浏览器窗口的宽度。
5、getBoundingClientRect()返回元素的大小及其相对于视口的位置。 这里获取的大小包括边框,内容和内边距。 获取相对于视口的位置时,都是视口到外边框的距离。
6、document.documentElement.scrollTop:获取滚动条滚动的高度。这个值是可以设置的。)
判断方法:
1、通过元素的位置信息和滚动条滚动的高度来判断
    function isContain(dom) {
    // 获取可视窗口的高度。
    const screenHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    // 获取滚动条滚动的高度
    const scrollTop = document.documentElement.scrollTop;
    // 获取元素偏移的高度。就是距离可视窗口的偏移量。
    const offsetTop = dom.offsetTop;
    return offsetTop - scrollTop <= screenHeight;
}
2、通过getBoundingClientRect方法来获取元素的位置信息,然后加以判断
首先来介绍一下getBoundingClientRect方法。
他是dom对象的一个方法。返回一个DOMRect对象。该对象拥有left, top, right, bottom, x, y, width, 和 height属性。
当页面发生滚动的时候,top, left, right, bottom属性值都会随之改变。
top:就是元素上外边框到视口顶端距离。
left:就是元素左外边框到视口左端距离。
bottom:就是元素下外边框到视口顶端距离。
right:就是元素右外边框到视口左端距离。
如果想要判断子元素是否在可视区域内,只需要:
top 大于等于 0
left 大于等于 0
bottom 小于等于视窗高度
right 小于等于视窗宽度
  function isContain(dom) {
      const totalHeight = window.innerHeight || document.documentElement.clientHeight;
      const totalWidth = window.innerWidth || document.documentElement.clientWidth;
      // 当滚动条滚动时,top, left, bottom, right时刻会发生改变。
      const { top, right, bottom, left } = dom.getBoundingClientRect();
      return (top >= 0 && left >= 0 && right <= totalWidth && bottom <= totalHeight);
    }
3、通过webAPI,Intersection Observer来实现监听。
Intersection Observer API 会注册一个回调函数,每当被监视的元素进入或者退出另外一个元素时(或者 viewport),或者两个元素的相交部分大小发生变化时,该回调方法会被触发执行。
const options = {
    root: // 用于检查目标的可见性。必须是目标元素的祖先节点。
    rootMargin: "上右下左" // 给祖先节点设置margin,等同于css中的margin。用来扩展或缩小`rootBounds`这个矩形的大小,从而影响`intersectionRect`交叉区域的大小。
    threshold: //表示当子元素和父元素覆盖多少时触发回调函数。
}

const observer = new IntersectionObserver((entries) => {
    // 当满足条件时搞一些事情
    // 这里通过判断entries[0].isIntersecting来判断是否在可视区域
}, options)

observer.observe(dom);
下面是相关例子,该例子就是如果子元素没有显示在当前可是窗口中时,窗口的背景颜色显示为绿色,反之显示为蓝色。
  <style>
    .div {
      height: 2000px;
    }

    p {
      height: 200px;
      background: red;
    }
  </style>
  
  <body>
  <div class="div"></div>

  <p id="p">我出现啦</p>
  
  <!-- 通过元素位置关系方法-->
  <script>
    function isContain(dom) {
      // 获取可视窗口的盖度。
      const screenHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
      // 获取滚动条滚动的高度
      const scrollTop = document.documentElement.scrollTop;
      // 获取元素偏移的高度。就是距离可视窗口的偏移量。
      const offsetTop = dom.offsetTop;
      return offsetTop - scrollTop <= screenHeight;
    }
    const p = document.getElementById("p");
    window.onscroll = () => {
      if (isContain(p)) {
        document.body.style.backgroundColor = 'blue'
      } else {
        document.body.style.backgroundColor = 'green'
      }
    }
  </script>

  <!-- 通过 getBoundingClientRect方法-->
  <script>
    // 只有当子元素全部出现在父元素中时,才会返回true。
    function isContain(dom) {
      const totalHeight = window.innerHeight || document.documentElement.clientHeight;
      const totalWidth = window.innerWidth || document.documentElement.clientWidth;
      // 当滚动条滚动时,top, left, bottom, right时刻会发生改变。
      const { top, right, bottom, left } = dom.getBoundingClientRect();
      console.log(top, right, bottom, left)
      return (top >= 0 && left >= 0 && right <= totalWidth && bottom <= totalHeight);
    }

    const p = document.getElementById("p");
    window.onscroll = () => {
      if (isContain(p)) {
        document.body.style.backgroundColor = 'blue'
      } else {
        document.body.style.backgroundColor = 'green'
      }
    }
  </script> 
  
  <!-- 通过new IntersectionObserver();  -->
  <script>
    const observer = new IntersectionObserver((entries) => {
      if (entries[0].isIntersecting) {
        document.body.style.backgroundColor = "blue"
      } else {
        document.body.style.backgroundColor = "green"
      }
    }, { threshold: .2 });
    const p = document.getElementById("p")
    observer.observe(p)
  </script>
</body>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值