Created
January 29, 2024 03:49
-
-
Save Verfranc/da85b73e30d50372f45a2c65effab7e8 to your computer and use it in GitHub Desktop.
Web API Snippets
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
=================================== | |
IntersectionObserver | |
-------------------- | |
function createImageObserver() { | |
lazyImages.length = 0; | |
lazyImages = [].slice.call(document.querySelectorAll('img[data-src]')); | |
if ('IntersectionObserver' in window) { | |
var lazyImageObserver = new IntersectionObserver(function(entries, observer) { | |
entries.forEach(function(entry) { | |
if (entry.isIntersecting) { | |
var lazyImage = entry.target; | |
lazyImage.src = lazyImage.dataset.src; | |
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