创建对象

var io = new IntersectionObserver(callback, option);

IntersectionObserver是浏览器原生提供的构造函数,接受两个参数:callback是可见性变化时的回调函数,option是配置对象(该参数可选)。

构造函数的返回值是一个观察器实例。实例的observe方法可以指定观察哪个 DOM 节点。

// 开始观察,参数是观察对象元素
io.observe(document.getElementById('example')); // 停止观察
io.unobserve(element); // 关闭观察器
io.disconnect();

如果要观察多个节点,就要多次调用这个方法。

io.observe(elementA);
io.observe(elementB);

callback 参数

目标元素的可见性变化时,就会调用观察器的回调函数callbackcallback一般会触发两次。一次是目标元素刚刚进入视口(开始可见),另一次是完全离开视口(开始不可见)。callback函数的参数(entries)是一个数组,每个成员都是一个IntersectionObserverEntry对象

const intersectionObserver = new IntersectionObserver((entries) => {
console.log(entries);# entries 返回数组
for (entry of entries)
{
if (entry.intersectionRatio > )
   {
    addAnimationClass(entry.target, animationClass);
}
else {
     console.log(animationClass); removeAnimationClass(entry.target, animationClass);
}
}
}

IntersectionObserverEntry对象一共有六个属性

  • boundingClientRect:目标元素的矩形区域的信息
  • intersectionRatio:目标元素的可见比例,即intersectionRect占boundingClientRect的比例,完全可见时为1,完全不可见时小于等于0
  • intersectionRect:目标元素与视口(或根元素)的交叉区域的信息
  • rootBounds:根元素的矩形区域的信息,getBoundingClientRect()方法的返回值,如果没有根元素(即直接相对于视口滚动),则返回null
  • target:被观察的目标元素,是一个 DOM 节点对象
  • time:可见性发生变化的时间,是一个高精度时间戳,单位为毫秒

Option 对象

IntersectionObserver构造函数的第二个参数是一个配置对象。它可以设置以下属性。

6.1 threshold 属性

threshold属性决定了什么时候触发回调函数。它是一个数组,每个成员都是一个门槛值,默认为[0],即交叉比例(intersectionRatio)达到0时触发回调函数。

new IntersectionObserver(
entries => {/* ... */},
{
threshold: [, 0.25, 0.5, 0.75, ]
}
);

用户可以自定义这个数组。比如,[0, 0.25, 0.5, 0.75, 1]就表示当目标元素 0%、25%、50%、75%、100% 可见时,会触发回调函数。

6.2 root 属性,rootMargin 属性

很多时候,目标元素不仅会随着窗口滚动,还会在容器里面滚动(比如在iframe窗口里滚动)。容器内滚动也会影响目标元素的可见性,IntersectionObserver API 支持容器内滚动。root属性指定目标元素所在的容器节点(即根元素)。注意,容器元素必须是目标元素的祖先节点。

var option = {
root: document.querySelector('.container'),
rootMargin: "500px 0px"
}; var observer = new IntersectionObserver(
callback,
option
);

上面代码中,除了root属性,还有rootMargin属性。后者定义根元素的margin,用来扩展或缩小rootBounds这个矩形的大小,从而影响intersectionRect交叉区域的大小。它使用CSS的定义方法,比如10px 20px 30px 40px,表示 top、right、bottom 和 left 四个方向的值。

这样设置以后,不管是窗口滚动或者容器内滚动,只要目标元素可见性变化,都会触发观察器。

惰性加载案例

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=es6"></script> -->
<title>IntersectionObserver</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
} div {
margin: 0 auto;
max-width: 100%;
width: 600px;
} #top {
height: 1200px;
background-color: #aaaaaa;
} #middle {
margin-top: 200px;
opacity: 0;
height: 400px;
background-color: #000000;
} #bottom {
height: 300px;
background-color: #333;
} #middle.move,
#bottom.move {
animation: movefromleft 2s;
animation-fill-mode: forwards;
} @keyframes movefromleft {
from {
opacity: 0;
transform: translateX(-300px);
}
to {
opacity: 1;
transform: translateX(0px);
}
}
</style>
</head> <body>
<div id="top"></div>
<div id="middle"></div>
<div id="bottom"></div>
</body>
<script>
window.onload = (event) => {
const middle = document.getElementById('middle'),
bottom = document.getElementById('bottom');
const animationClass = 'move';
// 创建监听
const intersectionObserver = new IntersectionObserver((entries) => {
console.log(entries);
for (entry of entries) {
if (entry.intersectionRatio > 0) {
addAnimationClass(entry.target, animationClass);
} else {
console.log(animationClass);
removeAnimationClass(entry.target, animationClass);
}
}
});
// 添加动画class的操作
function addAnimationClass(elem, animationClass) {
elem.className.includes(animationClass) ? 1 : elem.className = elem.className + ' ' + animationClass;
}
// 移除动画class的操作
function removeAnimationClass(elem, animationClass) {
elem.className.includes(animationClass) ? elem.className = elem.className.replace(animationClass, '') : 1;
console.log(elem.className);
}
// 开启监听
intersectionObserver.observe(middle);
intersectionObserver.observe(bottom);
} </script> </html>

单个元素(图片)

const eles = document.querySelectorAll('img[data-src]')
const observer = new IntersectionObserver( entries => {
entries.forEach(entry => {
if (entry.intersectionRatio > 0) {
let oImg = entry.target
oImg.src = oImg.getAttribute('data-src')
observer.unobserve(oImg)
}
})
}, {
root: document.getElementById('list')
})
eles.forEach(item => { observer.observe(item) })

多个图片(元素)

注意:

IntersectionObserver API 是异步的,不随着目标元素的滚动同步触发。

规格写明,IntersectionObserver的实现,应该采用requestIdleCallback(),即只有线程空闲下来,才会执行观察器。这意味着,这个观察器的优先级非常低,只在其他任务执行完,浏览器有了空闲才会执行。

文章引用:IntersectionObserver API 使用教程

      Intersection Observer

      图片优化

IntersectionObserver的更多相关文章

  1. IntersectionObserver API

    温馨提示:本文目前仅适用于在 Chrome 51 及以上中浏览. 2016.11.1 追加,Firefox 52 也已经实现. 2016.11.29 追加,Firefox 的人担心目前规范不够稳定,未 ...

  2. 使用IntersectionObserver更高效的监视某个页面元素是否进入了可见窗口

    比如说,你想跟踪 DOM 树里的一个元素,当它进入可见窗口时得到通知. 也许想实现即时延迟加载图片功能,或者你需要知道用户是否真的在看一个广告 banner. 你可以通过绑定 scroll 事件或者用 ...

  3. IntersectionObserver实现图片懒加载

    API: https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API 直接上源码: <!DOCTYPE ...

  4. IntersectionObserver API 使用教程

    转载:原文地址:http://www.ruanyifeng.com/blog/2016/11/intersectionobserver_api.html 网页开发时,常常需要了解某个元素是否进入了&q ...

  5. IntersectionObserver简介

    写在前面 在移动端,有个很重要的概念,叫做懒加载,适用于一些图片资源特别多,ajax数据特别多的页面中,经常会有动态加载数据的场景中,这个时候,我们通常是使用监听scroll或者使用setInterv ...

  6. [Javascript] IntersectionObserver -- Lazy Load Images on a Website

    When it comes to websites performance is king. How long it takes for a page to load can mean the dif ...

  7. IntersectionObserver API,观察元素是否进入了可视区域

    网页开发时,常常需要了解某个元素是否进入了"视口"(viewport),即用户能不能看到它. 上图的绿色方块不断滚动,顶部会提示它的可见性. 传统的实现方法是,监听到scroll事 ...

  8. 谈谈IntersectionObserver懒加载

    概念 IntersectionObserver接口(从属于Intersection Observer API)为开发者提供了一种可以异步监听目标元素与其祖先或视窗(viewport)交叉状态的手段.祖 ...

  9. js IntersectionObserver api

    API const options = { root: null, threshold: [0, 0.5, 1], rootMargin: '30px 100px 20px' } var io = n ...

随机推荐

  1. leetcode:Median of Two Sorted Arrays分析和实现

    这个问题的大意是提供两个有序的整数数组A与B,A与B并集的中间数.[1,3]与[2]的中间数为2,因为2能将A与B交集均分.而[1,3]与[2,4]的中间数为2.5,取2与3的平均值.故偶数数目的中间 ...

  2. 刷题向》关于第一篇状压DP BZOJ1087 (EASY+)

    这是本蒟蒻做的第一篇状压DP,有纪念意义. 这道题题目对状压DP十分友善,算是一道模板题. 分析题目,我们发现可以用0和1代表每一个格子的国王情况, 题目所说国王不能相邻放置,那么首先对于每一行是否合 ...

  3. Chrome,firefox,ie等浏览器空格&nbsp;宽度不一样

    方案一:使用其他字符代替空格 使用(&nbsp:)空格浏览器之间,显示的不一样,对不齐等现象. 解决方案: 用半角空格&ensp:或者全角空格&emsp:就可以了,&e ...

  4. c++ 多态问题(在虚函数里调用虚函数)

    最近在看cocos2d-x的源码,非常感激cocos2d作者的开源精神.在看代码的过程中感觉两个方向让我受益,1.把之前从书中看到的c++知识,明白了怎么运用.2.学习作者驾驭代码的巧妙方法. 看co ...

  5. Tensorflow训练结果测试

    代码参考(https://blog.csdn.net/disiwei1012/article/details/79928679) import osimport sysimport randomimp ...

  6. Python PyPI中国镜像

    from:http://blog.makto.me/post/2012-11-01/pypi-mirror from:http://www.pypi-mirrors.org/ from:http:// ...

  7. Java生产者消费者问题

    1. package interview.thread; import java.util.LinkedList; import java.util.Queue; import org.apache. ...

  8. Oracle——分组函数

    AVG(平均值)和 SUM (合计)函数 可以对数值型数据使用AVG 和 SUM 函数. AVG组函数忽略空值 --在组函数中使用NVL函数 --求平均值 )) MIN(最小值)和 MAX(最大值)函 ...

  9. Python基础-3

    目录 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 知识插入:嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 一.函数基本语法 函数是什么? 函数一词 ...

  10. 企业管理系统——第三周需求&原型改进_张正浩,黄锐斌

    企业管理系统——需求&原型改进,架构设计,测试计划 组员:张正浩,黄锐斌 一.需求&原型改进 1.给目标用户展现原型,与目标用户进一步沟通理解需求 我们的目标用户是公司管理员 场景:随 ...