图片懒加载

2022-01-02

前端

如果你只读每个人都在读的书,你也只能想到每个人都能想到的事。——村上春树

首先贴代码:

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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.ruben {
width: 400px;
height: 300px;
background-color: #e8e8e8
}

.ruben img {
width: 100%;
height: 100%;
}
</style>
</head>
<body>

<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3059_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3058_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3057_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3055_3.jpg"></div>
<div class="ruben"><img src="https://VampireAchao.github.io/imgs/preview/3056_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3054_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3053_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3052_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3051_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3050_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3049_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3048_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3047_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3046_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3045_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3044_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3043_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3042_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3041_3.jpg"></div>
<div class="ruben"><img data-src="https://VampireAchao.github.io/imgs/preview/3040_3.jpg"></div>

<script type="text/javascript">
// 获取需要懒加载的元素
let lazyLoadImgs = document.querySelectorAll("img")

function lazyLoad() {
// 屏幕可视区域高度
let clientHeight = document.documentElement.clientHeight
// 遍历图片
lazyLoadImgs.forEach(e => {
if (!e.src) {
// 获取元素界面信息(宽高位置等)
let rect = e.getBoundingClientRect()
if (rect.top < clientHeight) {
// 如果当前图片处于可视区域,则将data-src中的值放入src
e.src = e.dataset.src
}
}
})
// 滚动时加载
window.onscroll ??= lazyLoad
}
lazyLoad()
</script>
</body>
</html>

在你们自己的代码中使用方式也很简单:

首先复制这串js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 获取需要懒加载的元素
let lazyLoadImgs = document.querySelectorAll("img")

function lazyLoad() {
// 屏幕可视区域高度
let clientHeight = document.documentElement.clientHeight
// 遍历图片
lazyLoadImgs.forEach(e => {
if (!e.src) {
// 获取元素界面信息(宽高位置等)
let rect = e.getBoundingClientRect()
if (rect.top < clientHeight) {
// 如果当前图片处于可视区域,则将data-src中的值放入src
e.src = e.dataset.src
}
}
})
// 滚动时加载
window.onscroll ??= lazyLoad
}
lazyLoad()

然后在你们页面中使用,这里需要将你们原来页面上img标签的src改为data-src,这样在元素处于可视区域,则将data-src中的值放入src,然后达到懒加载的效果