来源:http://itssh.cn/post/934.html
IntersectionObserver 可以自动"观察"元素是否可见,Chrome 51+ 已经支持。由于可见(visible)的本质是,目标元素与视口产生一个交叉区,所以这个 API 叫做"交叉观察器"
使用IntersectionObserver实现图片懒加载功能:
ps:使用最新新版Chrome、Firefox测试,IE11及其以下暂不支持
案例:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<title>使用IntersectionObserver实现懒加载</title>
<!--
@author:sm
@email:sm0210@qq.com
@desc:使用IntersectionObserver实现懒加载
ps:使用最新新版Chrome、Firefox测试,IE11及其以下暂不支持
-->
<style type="text/css">
* {
margin: 0;
padding: 0;
}
ul,li {
list-style: none;
}
.list {
width: 800px;
margin: 0 auto;
}
.list ul {
width: 100%;
overflow: hidden;
}
.list ul li {
float: left;
width: 185px;
height: 400px;
margin-bottom: 10px;
margin-left: 10px;
background-color: #ccc;
overflow: hidden;
text-align: center;
line-height: 400px;
color: red;
font-size: 24px;
}
</style>
</head>
<body>
<!--
template:h5新标签,模板标签,不可见元素
-->
<div class="list">
<ul>
<li class="lazy-loaded">
<template>
![](images/1.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/2.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/3.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/4.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/5.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/6.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/7.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/8.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/9.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/10.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/1.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/2.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/3.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/4.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/5.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/6.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/1.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/2.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/3.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/4.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/5.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
![](images/6.jpg)
</template>
<span class="loading">正在加载...</span>
</li>
</ul>
</div>
</body>
<!-- -->
<script type="text/javascript">
//获取dom
function filterDom(selector) {
//
return Array.from(document.querySelectorAll(selector));
}
//事件观察者
var observer = new IntersectionObserver(observerCall);
//回调函数(可见性变化时的回调函数)
//回调被调用何时被调用:
//1.目标元素刚刚进入视口(开始可见)调用回调;
//2.另一次是完全离开视口(开始不可见)调用回调;
function observerCall(changes) {
changes.forEach(function(change) {
//使用setTimeout本地模拟加载速度,从服务器加载资源不需要添加setTimeout
setTimeout(function(){
//当前dom
var container = change.target;
//获取模板内容
var content = container.querySelector('template').content;
//追加元素
container.appendChild(content);
//移除loading
//container.querySelector('.loading').remove();
//隐藏loading
container.querySelector('.loading').style.display = 'none';
//停止观察,butin否则离开窗口可见区域也会执行callback
observer.unobserve(container);
//observer.disconnect(); 停止所有观察
}, 100);//end setTimout
});
}
//过滤元素
filterDom('.lazy-loaded').forEach(function (item) {
//开始观察
observer.observe(item);
});
</script>
</html>
来源:http://itssh.cn/post/934.html
效果: