-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlazyLoad.js
43 lines (43 loc) · 1.88 KB
/
lazyLoad.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
(function($){
$.extend($,{
imgLazyLoad:function(){
var timer,
len = $('img.lazyload').length;
function getPos(node) {
var scrollx = document.documentElement.scrollLeft || document.body.scrollLeft,
scrollt = document.documentElement.scrollTop || document.body.scrollTop;
var pos = node.getBoundingClientRect();
return {top:pos.top + scrollt, right:pos.right + scrollx, bottom:pos.bottom + scrollt, left:pos.left + scrollx }
}
function loading(){
timer && clearTimeout(timer);
timer = setTimeout(function(){
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop,
imgs=$('img.lazyload');
screenHeight = document.documentElement.clientHeight;
for(var i = 0;i < imgs.length;i++){
var pos = getPos(imgs[i]),
posT = pos.top,
posB = pos.bottom,
screenTop = screenHeight+scrollTop;
if((posT > scrollTop && posT < screenTop) || (posB > scrollTop && posB < screenTop)){
imgs[i].src = imgs[i].getAttribute('data-img');
$(imgs[i]).removeClass('lazyload');
}else{
// new Image().src = imgs[i].getAttribute('data-img');
}
}
},100);
}
if(!len) return;
loading();
$(window).on('scroll resize',function(){
if(!$('img.lazyload').length){
return;
}else{
loading();
}
})
}
})
})($||Zepto||jQuery);