SHARE
TWEET

Untitled

a guest Mar 26th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top