-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path懒加载 test.html
115 lines (106 loc) · 2.52 KB
/
懒加载 test.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
width: 1200px;
margin: auto;
padding: 10px 0;
border: 1px solid red;
overflow: hidden;
}
* {
margin: 0;
padding: 0;
list-style: none;
}
ul li {
float: left;
}
.box li {
box-sizing: border-box;
width: 280px;
height: 320px;
border: 1px solid red;
margin: 10px 10px;
}
</style>
</head>
<body>
<div class="box" id="box">
<ul>
<li>
<img src="">
</li>
<li>
<img src="">
</li>
<li>
<img src="">
</li>
<li>
<img src="">
</li>
<li>
<img src="">
</li>
<li>
<img src="">
</li>
<li>
<img src="">
</li>
<li>
<img src="">
</li>
<li>
<img src="">
</li>
<li>
<img src="">
</li>
<li>
<img src="">
</li>
<li>
<img src="">
</li>
<li>
<img src="">
</li>
<li>
<img src="">
</li>
</ul>
</div>
</body>
</html>
<script>
var oDiv = document.getElementsByTagName("div")[0]
//获取所有图片对象
var aImg=document.getElementsByTagName("img");
//获取可视区域高度
var showHeight=document.documentElement.clientHeight;
//滚动触发事件
window.onload=window.onscroll = function (ev) {
//得到卷起来的高度
var scrollTop= document.body.scrollTop
// 遍历所有图片
for (var i = 0; i <aImg.length ; i++) {
//获取所有图片距离网页顶部的距离
var top=aImg[i].offsetTop
//根据top 和卷起来的高度和可视区域高度
// 半段该图片是否需要显示
if (top <= showHeight+scrollTop){
aImg[i].src=aImg[i].getAttribute('_src')
}
}
}
console.log(document.documentElement.clientWidth)
console.log(document.documentElement.clientHeight)
</script>