-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazy.js
73 lines (64 loc) · 1.8 KB
/
lazy.js
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
// IntersectionObserver
document.addEventListener("DOMContentLoaded", () => {
if ("IntersectionObserver" in window) {
const imgs = document.getElementsByTagName("img");
const imageObserve = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
// 通过该属性判断元素是否出现在视口内
if (entry.isIntersecting) {
// entry.target能够取得那个dom元素
const img = entry.target;
img.src = img.dataset.src;
// 解除监视
imageObserve.unobserve(img);
}
});
});
[...imgs].forEach((img) => {
// 开启监视每一个元素
imageObserve.observe(img);
});
} else {
alert("您的浏览器不支持IntersectionObserver!");
}
});
// getBoundingClientRect
const imgs = document.getElementsByTagName('img');
// 判断元素是否出现在视口内
function isShow(el) {
// 视口高度
const clientH = window.innerHeight;
const bound = el.getBoundingClientRect();
// 判断元素左上角到视口顶部的距离是否小于视口高度
return bound.top < clientH;
};
// 加载图片
function showImg(el) {
if (!el.src) {
el.src = el.dataset.src;
}
}
// 懒加载
function lazyLoad() {
console.log('加载了');
[...imgs].forEach(el => {
if (isShow(el)) {
showImg(el);
}
})
};
lazyLoad();
// 节流函数
function throttle(fn, delay) {
let timer = null;
return () => {
if (timer) {
return;
};
timer = setTimeout(() => {
fn(imgs);
timer = null;
}, delay);
}
};
window.onscroll = throttle(lazyLoad, 500);