Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. document.addEventListener("DOMContentLoaded", function() {
  2. var lazyImages = [].slice.call(document.querySelectorAll("img.lazy"));
  3.  
  4. if ("IntersectionObserver" in window) {
  5. let lazyImageObserver = new IntersectionObserver(function(entries, observer) {
  6. entries.forEach(function(entry) {
  7. if (entry.isIntersecting) {
  8. let lazyImage = entry.target;
  9. lazyImage.src = lazyImage.dataset.src;
  10. lazyImage.srcset = lazyImage.dataset.srcset;
  11. lazyImage.classList.remove("lazy");
  12. lazyImageObserver.unobserve(lazyImage);
  13. }
  14. });
  15. });
  16.  
  17. lazyImages.forEach(function(lazyImage) {
  18. lazyImageObserver.observe(lazyImage);
  19. });
  20. } else {
  21. // Possibly fall back to a more compatible method here
  22. }
  23. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement