观察者(IntersectionObserver API)的基本使用(入门篇)

20 篇文章 0 订阅

用法可以参考https://developer.mozilla.org/zh-CN/docs/Web/API/IntersectionObserver

介绍

一般情况下监听可视区域或者几个元素的相对可视区域状态都不是很容易,传统方式也特别消耗性能,此时就可以使用到IntersectionObserver。

操作

操作为监听一个列表的首尾是否进入视窗。
定义一个new IntersectionObserver(callback,option)
注意:此文档只使用到了callback

观察者建造

 //观察者建造
 var observer = new IntersectionObserver((entries, observe) => {
  })

监听观察对象

可以监听指定的元素,也可以监听所有元素.
遍历所有的话就可以监听所有元素。

	//获取所有li
    var lis = document.querySelectorAll('li')
   // 监听第一个
    observer.observe(document.querySelectorAll('li')[0])
    // 监听最后一个
    observer.observe(lis[lis.length - 1])

补充:监听所有元素

    // 遍历所有的
    document.querySelectorAll("li").forEach(i=>{
      observer.observe(i)
    })

开始监听

  var observer = new IntersectionObserver((entries, observe) => {
      entries.forEach((i) => {
        // 通过滚动判断是否是当前的监听对象
        if (i.isIntersecting) {
          if (i.target.id == 'first') {
            first(i)
          } else if (i.target.id == 'last') {
            last(i)
          }
        }
      })
    })
    
    function first(val) {
      console.log('first')
    }
    function last() {
      console.log('last')
    }

完整代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #box {
        width: 200px;
        height: 200px;
        background: red;
        overflow-y: scroll;
        margin: 20px auto;
      }
      #box li {
        height: 30px;
        list-style: none;
        border-bottom: 1px solid #000;
      }
    </style>
  </head>
  <body>
    <ul id="box">
      <li id="first">1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
      <li>8</li>
      <li>9</li>
      <li>10</li>
      <li>11</li>
      <li>12</li>
      <li>13</li>
      <li>14</li>
      <li>15</li>
      <li>16</li>
      <li>17</li>
      <li>18</li>
      <li>19</li>
      <li id="last">20</li>
    </ul>
  </body>
  <script>
    var lis = document.querySelectorAll('li')
    var observer = new IntersectionObserver((entries, observe) => {
      entries.forEach((i) => {
        // 通过滚动判断是否是当前的监听对象
        if (i.isIntersecting) {
          if (i.target.id == 'first') {
            first(i)
          } else if (i.target.id == 'last') {
            last(i)
          }
        }
      })
    })

    // 监听第一个
    observer.observe(document.querySelectorAll('li')[0])
    // 监听最后一个
    observer.observe(lis[lis.length - 1])

    function first(val) {
      console.log('first')
    }
    function last() {
      console.log('last')
    }
  </script>
</html>

小案例

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #box {
        width: 200px;
        height: 200px;
        overflow-y: scroll;
        margin: 20px auto;
      }
      #box li {
        height: 30px;
        width: 100%;
        list-style: none;
        background-image: linear-gradient(#73ffa0, #73ffa0);
        background-repeat: no-repeat;
        background-size: 0% 38%;
        background-position: 0 70%;
      }
      #box li.animate {
        animation: changeBg 1s ease-in-out forwards;
      }
      @keyframes changeBg {
        to {
          background-size: 100% 38%;
        }
      }
    </style>
  </head>
  <body>
    <ul id="box">
      <li id="first">1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
      <li>8</li>
      <li>9</li>
      <li>10</li>
      <li>11</li>
      <li>12</li>
      <li>13</li>
      <li>14</li>
      <li>15</li>
      <li>16</li>
      <li>17</li>
      <li>18</li>
      <li>19</li>
      <li id="last">20</li>
    </ul>
  </body>
  <script>
    var lis = document.querySelectorAll('li')
    var observer = new IntersectionObserver((entries, observe) => {
      entries.forEach((i) => {
        // 通过滚动判断是否是当前的监听对象
        if (i.isIntersecting) {
          i.target.classList.add('animate')
          observer.unobserve(i.target)
        }
      })
    })
    // 遍历所有的
    document.querySelectorAll('li').forEach((i) => {
      observer.observe(i)
    })
  </script>
</html>

希望阅读完之后对您有所帮助。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hope°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值