mirror of
https://github.com/Anvilcraft/jensmemes
synced 2024-11-12 13:01:30 +01:00
29 lines
1.1 KiB
JavaScript
29 lines
1.1 KiB
JavaScript
|
document.addEventListener("DOMContentLoaded", function() {
|
||
|
var lazyloadImages = document.querySelectorAll("img.lazy");
|
||
|
var lazyloadThrottleTimeout;
|
||
|
|
||
|
function lazyload () {
|
||
|
if(lazyloadThrottleTimeout) {
|
||
|
clearTimeout(lazyloadThrottleTimeout);
|
||
|
}
|
||
|
|
||
|
lazyloadThrottleTimeout = setTimeout(function() {
|
||
|
var scrollTop = window.pageYOffset;
|
||
|
lazyloadImages.forEach(function(img) {
|
||
|
if(img.offsetTop < (window.innerHeight + scrollTop)) {
|
||
|
img.src = img.dataset.src;
|
||
|
img.classList.remove('lazy');
|
||
|
}
|
||
|
});
|
||
|
if(lazyloadImages.length == 0) {
|
||
|
document.removeEventListener("scroll", lazyload);
|
||
|
window.removeEventListener("resize", lazyload);
|
||
|
window.removeEventListener("orientationChange", lazyload);
|
||
|
}
|
||
|
}, 20);
|
||
|
}
|
||
|
|
||
|
document.addEventListener("scroll", lazyload);
|
||
|
window.addEventListener("resize", lazyload);
|
||
|
window.addEventListener("orientationChange", lazyload);
|
||
|
});
|