Created
August 28, 2019 19:03
-
-
Save jawakarD/1358d34b7ec703d881b62d267f5d21dc to your computer and use it in GitHub Desktop.
lazy-load-medaium
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
<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