-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.lazyload.js
33 lines (26 loc) · 969 Bytes
/
jquery.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
(function($){
$.fn.lazyload = function(options){
var opts = $.extend($.fn.lazyload.defaults, options);
var elements = this;
$(window).bind('scroll', function(e){
loadAboveTheFoldImages(elements, opts);
});
loadAboveTheFoldImages(elements, opts);
return this;
};
$.fn.lazyload.defaults = {threshold: 0};
function aboveTheFold(element, options){
var fold = $(window).height() + $(window).scrollTop();
return fold >= $(element).offset().top - (options['threshold']);
};
function loadOriginalImage(element){
$(element).attr('src', $(element).attr('original-src')).removeAttr('original-src');
};
function loadAboveTheFoldImages(elements, options){
elements.each(function(){
if (aboveTheFold(this, options) && ($(this).attr('original-src'))){
loadOriginalImage(this);
}
});
};
})(jQuery);