<!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>
<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>
<div class="list">
<ul>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</template>
<span class="loading">正在加载...</span>
</li>
<li class="lazy-loaded">
<template>
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif" width="100%" height="100%" border="0"/>
</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,{
root: null,
rootMargin : '0px 0px 20px 0px',
threshold:[0, 1]
});
function observerCall(changes) {
changes.forEach(function(change) {
setTimeout(function(){
if(change.intersectionRatio > 0){
var container = change.target;
var content = container.querySelector('template').content;
container.appendChild(content);
container.querySelector('.loading').style.display = 'none';
observer.unobserve(container);
}
}, 100);
});
}
//过滤元素
filterDom('.lazy-loaded').forEach(function (item) {
observer.observe(item);
});
</script>
</html>