來源:http://itssh.cn/post/934.html
IntersectionObserver 可以自動"觀察"元素是否可見轴踱,Chrome 51+ 已經支持牌芋。由于可見(visible)的本質是,目標元素與視口產生一個交叉區(qū)盆犁,所以這個 API 叫做"交叉觀察器"
使用IntersectionObserver實現(xiàn)圖片懶加載功能:
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實現(xiàn)懶加載</title>
<!--
@author:sm
@email:sm0210@qq.com
@desc:使用IntersectionObserver實現(xiàn)懶加載
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);
//回調函數(shù)(可見性變化時的回調函數(shù))
//回調被調用何時被調用:
//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否則離開窗口可見區(qū)域也會執(zhí)行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
效果: