-
Notifications
You must be signed in to change notification settings - Fork 3
/
slacker.js
56 lines (50 loc) · 1.59 KB
/
slacker.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
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Slacker, an image lazy load plugin for jQuery
* @requires jQuery v1.7 or later
* @author Aaron Draczynski
*
* Copyright (C) 2010-2012 Aaron Draczynski (aarondraczynski at gmail)
* http://github.com/aarondraczynski/slacker
* http://twitter.com/developer
*
* Released under the MIT License
* http://www.opensource.org/licenses/mit-license.php
*/
(function($) {
// Load images slightly outside of the viewport using the following threshold, in pixels
var sensitivity = 50;
$.fn.slacker = function() {
// On scroll and resize, determine which images to load and show them
$(window).on('scroll resize ajaxComplete', function() {
$('.l:visible').each(function() {
if (!$.scrollVert(this) && !$.scrollHoriz(this)) {
slackem($(this));
$(this).trigger('show');
}
});
return false;
}).trigger('scroll');
setTimeout(function() {
$(window).trigger('scroll');
}, 1200);
return this;
};
function slackem(elem) {
elem.one('show', function() {
if (elem.hasClass('l')) {
elem.css('opacity', 0);
// Bind fade-in animation to image load event
elem.on('load', function() {
elem.animate({'opacity': 1}, 120).off('load', arguments.callee);
});
elem.removeClass('l').attr('src', elem.data('src'));
}
});
};
$.scrollVert = function(e) {
return ($(window).height() + $(window).scrollTop()) <= ($(e).offset().top - sensitivity);
};
$.scrollHoriz = function(e) {
return ($(window).width() + $(window).scrollLeft()) <= $(e).offset().left;
};
})(jQuery);