Last active
January 9, 2019 14:35
-
-
Save nikola0203/14fac8298123833aae525fa92e5746b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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