<!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>
使用IntersectionObserver让html滚动到可见区域图片懒加载
最新推荐文章于 2024-09-05 10:28:36 发布
本文介绍了一种使用IntersectionObserver API实现的图片懒加载技术,通过监听DOM元素的可见性来动态加载图片,有效提升网页加载速度和用户体验。文章详细展示了如何通过JavaScript代码实现这一功能,并提供了完整的HTML和CSS样式代码。
摘要由CSDN通过智能技术生成