Skip to content

Instantly share code, notes, and snippets.

@Blucknote
Last active June 10, 2020 10:52
Show Gist options
  • Save Blucknote/231831a16080b3aef5b26ad389ff260e to your computer and use it in GitHub Desktop.
Save Blucknote/231831a16080b3aef5b26ad389ff260e to your computer and use it in GitHub Desktop.
self descripted name
const lazyLoad = function() {
let lazyImages = $("img.lazy:visible");
setTimeout(function() {
$.each(
lazyImages,
function() {
let lazyImage = $(this)[0];
if ((lazyImage.getBoundingClientRect().top <= window.innerHeight && lazyImage.getBoundingClientRect().bottom >= 0) && getComputedStyle(lazyImage).display !== "none") {
lazyImage.src = lazyImage.dataset.src;
lazyImage.srcset = lazyImage.dataset.srcset;
lazyImage.classList.remove("lazy");
lazyImages = lazyImages.filter(function(image) {
return image !== lazyImage;
});
}
}
);
}, 200);
};
document.addEventListener("DOMContentLoaded", function() {
lazyLoad();
document.addEventListener("scroll", lazyLoad);
window.addEventListener("resize", lazyLoad);
window.addEventListener("orientationchange", lazyLoad);
});
<img
class="lazy img-fluid"
data-src="deffered.jpg"
data-srcset="idkdeffered.jpg"
src="temporary.jpg"
alt="something"
title="something"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment