-
Notifications
You must be signed in to change notification settings - Fork 213
/
lazyload.html
122 lines (116 loc) · 3.65 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
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
116
117
118
119
120
121
122
<!DOCTYPE html>
<head>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
<title>demo lazyload</title>
<meta charset="utf-8">
<style type="text/css">
* {
padding: 0;
margin: 0;
text-decoration: none;
list-style: none;
}
.lazyload img {
width: 640px;
}
.lazyload{
width: 700px;
margin: 0 auto;
text-align: center;
}
@media screen and (max-width: 980px) {
.lazyload img {
width: 100%;
height: 480px;
}
.lazyload{
width: 100%;
margin: 0 auto;
text-align: center;
}
}
</style>
</head>
<body>
<div class="lazyload">
<div>
<ul>
<li class="item">
<a href="javascript:void(0)"><img data-img="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher1.jpg" src="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher1.jpg"></a>
</li>
<li class="item">
<a href="javascript:void(0)"><img data-img="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher2.jpg" src="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher1.jpg"></a>
</li>
<li class="item">
<a href="javascript:void(0)"><img data-img="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher3.jpg" src="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher1.jpg"></a>
</li>
<li class="item">
<a href="javascript:void(0)"><img data-img="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher4.jpg" src="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher1.jpg"></a>
</li>
<li class="item">
<a href="javascript:void(0)"><img data-img="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher5.jpg" src="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher1.jpg"></a>
</li>
<li class="item">
<a href="javascript:void(0)"><img data-img="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher6.jpg" src="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher1.jpg"></a>
</li>
<li class="item">
<a href="javascript:void(0)"><img data-img="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher7.jpg" src="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher1.jpg"></a>
</li>
<li class="item">
<a href="javascript:void(0)"><img data-img="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher8.jpg" src="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher1.jpg"></a>
</li>
<li class="item">
<a href="javascript:void(0)"><img data-img="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher9.jpg" src="http://h5.sztoda.cn/static/img/loveLetter/teacher/teacher1.jpg"></a>
</li>
</ul>
</div>
</div>
<script type="text/javascript">
var lazyLoad = (function(){
var clock;
function init(){
$(window).on("scroll", function(){
if (clock) {
clearTimeout(clock);
}
clock = setTimeout(function(){
checkShow();
}, 200);
})
checkShow();
}
function checkShow(){
$(".lazyload img").each(function(){
var $cur =$(this);
if($cur.attr('isLoaded')){
return;
}
if(shouldShow($cur)){
showImg($cur);
}
})
}
function shouldShow($node){
var scrollH = $(window).scrollTop(),
winH = $(window).height(),
top = $node.offset().top;
if(top < winH + scrollH){
return true;
}else{
return false;
}
}
function showImg($node){
$node.hide()
$node.attr('src', $node.attr('data-img'));
$node.fadeIn()
$node.attr('isLoaded', true);
}
return {
init: init
}
})()
lazyLoad.init();
</script>
</body>
</html>