Skip to content

Instantly share code, notes, and snippets.

@jawakarD
Created August 28, 2019 19:03
Show Gist options
  • Save jawakarD/1358d34b7ec703d881b62d267f5d21dc to your computer and use it in GitHub Desktop.
Save jawakarD/1358d34b7ec703d881b62d267f5d21dc to your computer and use it in GitHub Desktop.
lazy-load-medaium
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
var lazyImages = [].slice.call(document.querySelectorAll("img.lazy"));
if ("IntersectionObserver" in window) {
let lazyImageObserver = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
let lazyImage = entry.target;
lazyImage.src = lazyImage.dataset.src;
lazyImage.classList.remove("lazy");
lazyImageObserver.unobserve(lazyImage);
}
});
},
{
rootMargin: "0px 0px 500px 0px"
});
lazyImages.forEach(function(lazyImage) {
lazyImageObserver.observe(lazyImage);
});
} else {
// Possibly fall back to a more compatible method here
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment