-
Notifications
You must be signed in to change notification settings - Fork 0
/
LazyLoad.html
88 lines (80 loc) · 3.99 KB
/
LazyLoad.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
img {
display: block;
margin-bottom: 50px;
width: 400px;
height: 400px;
}
</style>
</head>
<html>
<body>
<img src="default.jpg" data-src="http://ww4.sinaimg.cn/large/006y8mN6gw1fa5obmqrmvj305k05k3yh.jpg" alt="">
<img src="default.jpg" data-src="http://ww4.sinaimg.cn/large/006y8mN6gw1fa5obmqrmvj305k05k3yh.jpg" alt="">
<img src="default.jpg" data-src="http://ww1.sinaimg.cn/large/006y8mN6gw1fa7kaed2hpj30sg0l9q54.jpg" alt="">
<img src="default.jpg" data-src="http://ww1.sinaimg.cn/large/006y8mN6gw1fa7kaed2hpj30sg0l9q54.jpg" alt="">
<img src="default.jpg" data-src="http://ww4.sinaimg.cn/large/006y8mN6gw1fa5obmqrmvj305k05k3yh.jpg" alt="">
<img src="default.jpg" data-src="http://ww4.sinaimg.cn/large/006y8mN6gw1fa5obmqrmvj305k05k3yh.jpg" alt="">
<img src="default.jpg" data-src="http://ww4.sinaimg.cn/large/006y8mN6gw1fa5obmqrmvj305k05k3yh.jpg" alt="">
<img src="default.jpg" data-src="http://ww4.sinaimg.cn/large/006y8mN6gw1fa5obmqrmvj305k05k3yh.jpg" alt="">
<img src="default.jpg" data-src="http://ww1.sinaimg.cn/large/006y8mN6gw1fa7kaed2hpj30sg0l9q54.jpg" alt="">
<img src="default.jpg" data-src="http://ww4.sinaimg.cn/large/006y8mN6gw1fa5obmqrmvj305k05k3yh.jpg" alt="">
<img src="default.jpg" data-src="http://ww4.sinaimg.cn/large/006y8mN6gw1fa5obmqrmvj305k05k3yh.jpg" alt="">
<img src="default.jpg" data-src="https://cdn.pixabay.com/photo/2015/09/09/16/05/forest-931706_1280.jpg" alt="">
<img src="default.jpg" data-src="https://cdn.pixabay.com/photo/2014/08/01/00/08/pier-407252_1280.jpg" alt="">
<img src="default.jpg" data-src="https://cdn.pixabay.com/photo/2014/12/15/17/16/pier-569314_1280.jpg" alt="">
<img src="default.jpg" data-src="https://cdn.pixabay.com/photo/2010/12/13/10/09/abstract-2384_1280.jpg" alt="">
<img src="default.jpg" data-src="https://cdn.pixabay.com/photo/2015/10/24/11/09/drop-of-water-1004250_1280.jpg">
</body>
<script>
var num = document.getElementsByTagName('img').length;
var img = document.getElementsByTagName("img");
var current = 0; //存储图片加载到的位置,避免每次都从第一张图片开始遍历
lazyLoad(); //页面载入完毕加载可是区域内的图片
window.addEventListener('scroll', throttle(lazyLoad, 200))
function lazyLoad() {
var visibleHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
for (var i = current; i < num; i++) {
if (img[i].getBoundingClientRect().top < visibleHeight) {
if (img[i].getAttribute('src') === 'default.jpg') {
img[i].src = img[i].getAttribute('data-src')
}
current = i + 1
}
}
}
function throttle(func, time) {
let timer = null;
return function (...args) {
let context = this
if (!timer) {
timer = setTimeout(() => {
timer = null;
func.apply(context, args);
}, time);
}
}
}
// 方法二 IntersectionObserver
// if (IntersectionObserver) {
// let lazyImageObserver = new IntersectionObserver((entries, observer) => {
// entries.forEach((entry, index) => {
// let lazyImage = entry.target;
// // 如果元素可见
// if (entry.intersectionRatio > 0) {
// if (lazyImage.getAttribute("src") == "loading.gif") {
// lazyImage.src = lazyImage.getAttribute("data-src");
// }
// lazyImageObserver.unobserve(lazyImage)
// }
// })
// })
// for (let i = 0; i < img.length; i++) {
// lazyImageObserver.observe(img[i]);
// }
// }
</script>
</html>