IntersectionObserver是什么?

IntersectionObserver.png

  • IntersectionObserver概览
  • IntersectionObserver构造器
  • IntersectionObserver方法
  • IntersectionObserver懒加载(vue单文件组件版)
  • IntersectionObserver吸顶(vue单文件组件版)
  • IntersectionObserver触底(vue单文件组件版)
  • IntersectionObserver懒加载、吸顶、触底综合(vue单文件组件版)
  • 基于Intersection的开源工具 Scrollama
  • 总结
  • 参考资料

IntersectionObserver概览

  • IntersectionObserver提供了一个方式去异步观察 有一个祖先element或者top-level document viewport的目标element 的交叉变化。
  • 祖先元素或者viewport被当作root节点。
  • 当IntersectionObserver建立出的时候,它会被配置到监听root内部的给定visibility的变化。
  • 一旦IntersectionObserver建立出来,它的配置是不能变的。 因此一个observer object只能用来监测一个指定visibility值的变化。
  • 虽然只能一对一去watch ratio,可是能够在同一个observer中watch多个target elements。也就是说一个visibility ratio能够检测多个不一样的elements。

IntersectionObserver构造器

var observer = new IntersectionObserver(callback[, options]);css

  • 和其余构造器同样,建立并返回一个新的Intersection对象。
  • rootMargin若是指定一个特殊值,是为了确保语法是否正确
  • 阀值是为了确保值在0.0到1.0之间,threshold会按照升序排列。若threshold是空,值为[0.0]。

参数

  • callback 当目标元素的透明度穿过设定的threshold值时,函数会被调用。callback接受两个 参数。vue

    • entries 传入各个threshold值的数组,比该阀值指定的百分比更明显或者更不明显。
    • observer 调用callback的observer实例。
  • options 若options没设置。observer使用document的viewport做为root,没有margin,0%的threshold(意味着即便有1 px的变化也会触发回调)git

    • root 被当作viewport的元素。
    • rootMargin 语法是"0px 0px 0px 0px"
    • threshold 指明被监测目标总绑定盒模型的交叉区域ratio,值在0.0到1.0之间;0.0意味着即便是1px也会被当作可见的。1.0意味着整个元素是可见的。默认threshold值是0.0。

IntersectionObserver方法

  • IntersectionObserver.disconnect() 中止observe一个目标。
  • IntersectionObserver.observe() 告诉IntersectionObserver一目标元素去observe。
  • IntersectionObserver.takeRecords() 返回包含全部observe的对象一个数组。
  • IntersectionObserver.unobserve() 取消observe一个目标对象。

示例

下面的例子在threshold值变化在10%以上时触发myObserverCallback。github

let observer = new IntersectionObserver(myObserverCallback, { "threshold": 0.1 });

IntersectionObserver懒加载(vue单文件组件版)

<template>
  <div>
    <img v-for="(image, i) in images" :key="i" src :data-img-url="image" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [
        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247890587&di=88d4066be3d57ac962a6bec37e265d37&imgtype=0&src=http%3A%2F%2F01.imgmini.eastday.com%2Fmobile%2F20170810%2F20170810151144_d41d8cd98f00b204e9800998ecf8427e_3.jpeg',
        'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4054762707,1853885380&fm=26&gp=0.jpg',
        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247912077&di=508a949e5e291875debf6ca844292cd4&imgtype=0&src=http%3A%2F%2F03imgmini.eastday.com%2Fmobile%2F20180827%2F20180827095359_6759372e9bd28026ee6f53b500fb4291_2.jpeg',
      ],
    };
  },
  mounted() {
    const images = document.querySelectorAll('img');

    const observerLazyLoad = new IntersectionObserver((entries) => {
      entries.forEach((item) => {
        if (item.isIntersecting) {
          item.target.src = item.target.dataset.imgUrl;
        }
      });
    });

    images.forEach((image) => {
      observerLazyLoad.observe(image);
    });
  },
};
</script>

<style lang="scss" scoped>
img {
  display: block;
  height: 500px;
  margin: 30px;
}
</style>

IntersectionLazyLoad.gif

IntersectionObserver吸顶(vue单文件组件版)

<template>
  <div>
    <p class="fixed-top-helper"></p>
    <p class="fixed-top-reference"></p>
    <header>头部</header>
    <main>
      <img v-for="(image, i) in images" :key="i" :src="image" />
    </main>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [
        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247890587&di=88d4066be3d57ac962a6bec37e265d37&imgtype=0&src=http%3A%2F%2F01.imgmini.eastday.com%2Fmobile%2F20170810%2F20170810151144_d41d8cd98f00b204e9800998ecf8427e_3.jpeg',
        'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4054762707,1853885380&fm=26&gp=0.jpg',
        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247912077&di=508a949e5e291875debf6ca844292cd4&imgtype=0&src=http%3A%2F%2F03imgmini.eastday.com%2Fmobile%2F20180827%2F20180827095359_6759372e9bd28026ee6f53b500fb4291_2.jpeg',
      ],
    };
  },
  mounted() {
    const header = document.querySelector('header');
    const fixedTopReference = document.querySelector('.fixed-top-reference');
    fixedTopReference.style.top = `${header.offsetTop}px`;

    const observerFixedTop = new IntersectionObserver((entries) => {
      entries.forEach((item) => {
        if (item.boundingClientRect.top < 0) {
          header.classList.add('fixed');
        } else {
          header.classList.remove('fixed');
        }
      });
    });
    observerFixedTop.observe(fixedTopReference);
  },
};
</script>

<style lang="scss" scoped>
.fixed-top-helper {
  height: 1px;
  background: #ccc;
}
header {
  background: #ccc;
  &.fixed {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
  }
}
main {
  img {
    display: block;
    height: 500px;
    margin: 30px;
  }
}
footer {
  background: #ccc;
}
</style>

IntersectionObserverFixedTop.gif

注意事项:数组

  • fixedTopReference是为了不缓慢移动时add remove .fixed死循环,死循环的结果是抖动
  • fixedTopHelper是为了不被吸顶元素没有上一个sibling元素(也就是说被吸顶元素是最上层元素)时,避免缓缓移动时add remove .fixed死循环抖动,特殊引入的标签,须要设置1个px的height
  • fixedTopHelper须要与被吸顶元素保持样式一致,以确保好的用户体验。例如在本例中将其background设置为#ccc,很好的作到了隐藏

吸顶抖动

dance.gif

IntersectionObserver触底(vue单文件组件版)

<template>
  <div>
    <main>
      <img v-for="(image, i) in images" :key="i" src="image" />
    </main>
    <footer>底部</footer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [
        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247890587&di=88d4066be3d57ac962a6bec37e265d37&imgtype=0&src=http%3A%2F%2F01.imgmini.eastday.com%2Fmobile%2F20170810%2F20170810151144_d41d8cd98f00b204e9800998ecf8427e_3.jpeg',
        'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4054762707,1853885380&fm=26&gp=0.jpg',
        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247912077&di=508a949e5e291875debf6ca844292cd4&imgtype=0&src=http%3A%2F%2F03imgmini.eastday.com%2Fmobile%2F20180827%2F20180827095359_6759372e9bd28026ee6f53b500fb4291_2.jpeg',
      ],
    };
  },
  mounted() {
    const footer = document.querySelector('footer');

    const observerTouchBottom = new IntersectionObserver((entries) => {
      entries.forEach((item) => {
        if (item.isIntersecting) {
          setTimeout(() => {
            console.log('滚动到了底部,能够发request请求数据了');
          }, 2000);
        }
      });
    });

    observerTouchBottom.observe(footer);
  },
};
</script>

<style lang="scss" scoped>
main {
  img {
    display: block;
    height: 500px;
    margin: 30px;
  }
}
footer {
  background: #ccc;
}
</style>

IntersectionObserverTouchBottom.gif

IntersectionObserver懒加载、吸顶、触底综合(vue单文件组件版)

<template>
  <div>
    <p class="fixed-top-helper"></p>
    <p class="fixed-top-reference"></p>

    <header>头部</header>
    <main>
      <img v-for="(image, i) in images" :key="i" src :data-img-url="image" />
    </main>
    <footer>底部</footer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [
        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247890587&di=88d4066be3d57ac962a6bec37e265d37&imgtype=0&src=http%3A%2F%2F01.imgmini.eastday.com%2Fmobile%2F20170810%2F20170810151144_d41d8cd98f00b204e9800998ecf8427e_3.jpeg',
        'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4054762707,1853885380&fm=26&gp=0.jpg',
        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574247912077&di=508a949e5e291875debf6ca844292cd4&imgtype=0&src=http%3A%2F%2F03imgmini.eastday.com%2Fmobile%2F20180827%2F20180827095359_6759372e9bd28026ee6f53b500fb4291_2.jpeg',
      ],
    };
  },
  mounted() {
    const images = document.querySelectorAll('img');

    const observerLazyLoad = new IntersectionObserver((entries) => {
      entries.forEach((item) => {
        if (item.isIntersecting) {
          item.target.src = item.target.dataset.imgUrl;
        }
      });
    });

    images.forEach((image) => {
      observerLazyLoad.observe(image);
    });

    const footer = document.querySelector('footer');

    const observerTouchBottom = new IntersectionObserver((entries) => {
      entries.forEach((item) => {
        if (item.isIntersecting) {
          setTimeout(() => {
            console.log('滚动到了底部,能够发request请求数据了');
          }, 2000);
        }
      });
    });

    observerTouchBottom.observe(footer);

    const header = document.querySelector('header');
    const fixedTopReference = document.querySelector('.fixed-top-reference');
    fixedTopReference.style.top = `${header.offsetTop}px`;

    const observerFixedTop = new IntersectionObserver((entries) => {
      entries.forEach((item) => {
        if (item.boundingClientRect.top < 0) {
          header.classList.add('fixed');
        } else {
          header.classList.remove('fixed');
        }
      });
    });

    observerFixedTop.observe(fixedTopReference);
  },
};
</script>

<style lang="scss" scoped>
.fixed-top-helper {
  height: 1px;
  background: #ccc;
}
header {
  background: #ccc;
  &.fixed {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
  }
}
main {
  img {
    display: block;
    height: 500px;
    margin: 30px;
  }
}
footer {
  background: #ccc;
}
</style>

基于Intersection的开源工具 Scrollama

官方提供了Basic Process,Progress,Sticky Side,Sticky Overlay几种示例。异步

Basic Process

basic.gif

Progress

Progress.gif

Sticky Side

StickySide.gif

Sticky Overlay

StickyOverlay.gif

在项目中能够当作适当引用。ide

项目地址:https://github.com/russellgol...
demo地址:https://russellgoldenberg.git...函数

总结

  • 主要使用IntersectionObserver实现懒加载图片,触底,吸顶
  • 虽然vue单文件组件版本,可是我相信聪明的你知道核心部分在哪里
  • IntersectionObserver可能还会有其余的用处,来日方长,慢慢探索

参考资料

https://developer.mozilla.org...
https://developer.mozilla.org...
https://developer.mozilla.org...
https://juejin.im/post/5ca15c...
https://medium.com/walmartlab...
https://juejin.im/post/5d6651...
https://github.com/russellgol...工具

原文地址:IntersectionObserver是什么?post

相关文章
相关标签/搜索