-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg-lazy-loader.js
74 lines (65 loc) · 2 KB
/
img-lazy-loader.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* EXAMPLES:
*
* Shopify: <img src="{{ 'placeholder.jpg' | img_url }}" class="lazy-img" data-src="{{ 'actual-image' | img_url }}"/>
*
* Wordpress: <img src="<?= get_template_directory() . '<path to placeholder image>' ?>" class="lazy-img" data-src="<?= $actual_img ?>"/>
*
* CSS Background: <div class="lazy-bg" data-src="<?= get_template_directory() . '<path to placeholder image>' ?>"></div>
*/
var debounce = function(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
(function($){
var lazyImages = function () {
$first = $('.lazy-img').first();
if ($first.offset().top <= 0) {
$first.attr('src', $first.data('src'));
$first.removeClass('lazy-img');
}
debounce(
$(window).on('scroll', function() {
$('img.lazy-img').each(function(){
if ($(this).offset().top < ($(window).scrollTop() + $(window).height())) {
$(this).attr('src', $(this).data('src'));
$(this).removeClass('lazy-img');
}
});
}),
400,
false
)
}
var lazyBackgrounds = function () {
$first = $('.lazy-bg').first();
$first.css('background-image', 'url(' + $first.data('src') + ')');
$first.removeClass('lazy-bg');
debounce(
$(window).on('scroll', function() {
$('.lazy-bg').each(function(){
if ($(this).offset().top < ($(window).scrollTop() + $(window).height())) {
$(this).css('background-image', 'url(' + $(this).data('src') + ')');
$(this).removeClass('lazy-bg');
}
});
}),
400,
false
)
}
document.addEventListener("DOMContentLoaded", function() {
lazyImages();
lazyBackgrounds();
});
})(jQuery)