blob: f7bceb5e747653c445e085fd16b03ad99fd71af3 (
plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
var animationElements = [];
var imageElements = [];
var animationElementName = ".small-load";
// Hookable function
var loadAnimation = function (item) {
let img = new Image();
img.src = item.children[0].children[0].dataset.src;
img.onload = function () {
item.classList.remove("small-load", "medium-load", "large-load");
item.classList.add("small-loaded", "medium-loaded", "large-loaded");
}
}
// Hookable function
var loadImage = function (index) {
if (index >= imageElements.length) return;
let item = imageElements[index];
let image = new Image();
item.src = item.dataset.src;
image.src = item.src;
// if the image is loaded, load the next image
image.onload = function () {
loadImage(index + 1);
}
// if the image is not loaded, load the next image
image.onerror = function () {
loadImage(index + 1);
}
}
function initImage() {
// get all the images with data-src attribute
imageElements = document.querySelectorAll('img[data-src]')
// load the images one by one
loadImage(0);
animationElements = document.querySelectorAll(animationElementName);
// load the images which are in the viewport
viewPortLoad(0);
const debouncedHandleScroll = debounce(lazyAnimation, 10);
// add the event listener
window.addEventListener('scroll', debouncedHandleScroll);
}
function viewPortLoad(index) {
if (index >= animationElements.length) return;
let item = animationElements[index];
if (!isElementInView(item)) {
viewPortLoad(index + 1)
return;
};
loadAnimation(item)
viewPortLoad(index + 1);
}
function lazyAnimation() {
images = document.querySelectorAll(animationElementName);
viewPortLoad(0);
}
// check if the element is in the viewport
function isElementInView(element) {
const rect = element.getBoundingClientRect();
const elementTop = rect.top;
const elementBottom = rect.bottom;
return (elementTop >= 0 && elementBottom - 200 <= window.innerHeight);
}
function debounce(fn, delay) {
let timer = null;
return function () {
let context = this;
let args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
}
|