-
Notifications
You must be signed in to change notification settings - Fork 23
/
5.html
135 lines (110 loc) · 2.29 KB
/
5.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
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE html>
<html>
<head>
<script src="http://kejian.jirengu.com/static/js/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title>懒加载-封装</title>
<style>
ul,li{
list-style:none;
}
.container{
width: 800px;
margin: 0 auto;
}
p{
line-height: 30px;
}
</style>
</head>
<body>
<p>hello3</p>
<p>hello4</p>
<p>hello5</p>
<p>hello6</p>
<p>hello7</p>
<p>hello9</p>
<p>hello0</p>
<p>hello11</p>
<p>hello12</p>
<p>hello13</p>
<p>hello14</p>
<p>hello15</p>
<p>hello16</p>
<p>hello17</p>
<p>hello18</p>
<p>hello19</p>
<p>hello20</p>
<p>hello21</p>
<p>hello22</p>
<p>hello23</p>
<a href="#"><img src="http://kejian.jirengu.com/data/fe/%E8%AF%BE%E4%BB%B6/32-%E6%87%92%E5%8A%A0%E8%BD%BD/code/blank.jpg" data-src="http://kejian.jirengu.com/data/fe/%E8%AF%BE%E4%BB%B6/32-%E6%87%92%E5%8A%A0%E8%BD%BD/code/girl.jpg" /></a>
<script type="text/javascript">
function showImg($el){
$el.attr('src', $el.attr('data-src'));
}
var Exposure = (function(){
var _queue = [];
var _isBind = false;
function one ($selectors, callback){
_add($selectors, callback);
_init();
}
function _add ($selector, callback){
$selector.each(function(){
$cur = $(this);
var o = {
el: $cur,
cb: callback
};
_queue.push(o);
});
}
function _init(){
if(!_isBind){
_bind();
}
_do();
}
function _bind(){
var timer = null,
interval = 40;
$(window).on( 'scroll', function(e){
timer && clearTimeout( timer );
timer = setTimeout( function(){
_do();
}, interval );
} );
_isBind = true;
}
function _do(){
var arrTmp = [];
for(var i=0; i<_queue.length; i++){
var item = _queue[i];
if(_isShow(item.el)){
item.cb.call(item.el[0]);
}else{
arrTmp.push(item);
}
}
_queue = arrTmp;
}
function _isShow($el){
var scrollH = $(document).scrollTop(),
winH = $(window).height(),
top = $el.offset().top ;
return (top < winH + scrollH) ? true : false;
}
return {
one: one
}
})();
var $imgs = $('img');
Exposure.one($imgs, function(){
showImg($(this));
});
// Exposure.every($imgs, function(){
// })
</script>
</body>
</html>