-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlazy.js
More file actions
31 lines (30 loc) · 942 Bytes
/
Copy pathlazy.js
File metadata and controls
31 lines (30 loc) · 942 Bytes
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
$(function() {
function lazyLoad(x) {
x += $(window).height() + 100;
$('[data-background],[data-image]').each(function(){
var self = $(this);
if(self.offset().top > x) {
return;
}
if(self.data('background')){
var image = self.data('background');
} else {
var image = self.data('image');
}
$('<img>',{src:image}).on('load', function(){
if(self.data('background')){
self.css('background-image', 'url("'+image+'")');
} else {
self.attr('src', image);
}
self.removeAttr('data-background');
self.removeAttr('data-image');
})
});
}
$(window).scroll(function(){
var x = $(this).scrollTop();
lazyLoad(x);
});
lazyLoad(0);
});