-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.lazyLoading.js
38 lines (38 loc) · 1.05 KB
/
jquery.lazyLoading.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
(function($) {
$.fn.lazyLoading = function(options) {
var $this, checkImages, checking, settings;
settings = $.extend({}, {
sourceAttr: "data-src",
effectTime: 1000
}, options);
$this = this;
checking = false;
checkImages = function() {
if (checking) {
return;
}
checking = true;
$($this).filter(function(i) {
return $($this[i]).attr(settings.sourceAttr) !== void 0;
}).each(function(i, v) {
var rect, src;
rect = v.getBoundingClientRect();
if (rect.top >= 0 && rect.left >= 0 && rect.top <= (window.innerHeight || document.documentElement.clientHeight)) {
$(v).css("opacity", 0);
src = v.getAttribute(settings.sourceAttr);
v.removeAttribute(settings.sourceAttr);
v.src = src;
$(v).animate({
"opacity": 1
}, settings.effectTime);
}
});
checking = false;
};
checkImages();
$(window).scroll(function() {
checkImages();
});
return this;
};
})(jQuery);