Skip to content

Instantly share code, notes, and snippets.

@nikola0203
Last active January 9, 2019 14:35
Show Gist options
  • Save nikola0203/14fac8298123833aae525fa92e5746b0 to your computer and use it in GitHub Desktop.
Save nikola0203/14fac8298123833aae525fa92e5746b0 to your computer and use it in GitHub Desktop.
function PREFIX_lazy_load_images(image_selector) {
var lazyImages = [].slice.call(document.querySelectorAll(image_selector));
if ("IntersectionObserver" in window && "IntersectionObserverEntry" in window && "intersectionRatio" in window.IntersectionObserverEntry.prototype) {
let lazyImageObserver = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
let lazyImage = entry.target;
lazyImage.src = lazyImage.dataset.src;
// lazyImage.srcset = lazyImage.dataset.srcset;
// lazyImage.classList.remove("lazy-image");
// lazyImageObserver.unobserve(lazyImage);
}
});
});
lazyImages.forEach(function(lazyImage) {
lazyImageObserver.observe(lazyImage);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment