Created
February 13, 2021 21:05
-
-
Save chris-castillo-dev/a2e118f5650aeaf55a814d18d5700a5b to your computer and use it in GitHub Desktop.
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
document.addEventListener("DOMContentLoaded", function() { | |
var lazyBackgrounds = [].slice.call(document.querySelectorAll(".lazy-bg")); | |
if ("IntersectionObserver" in window && "IntersectionObserverEntry" in window && "intersectionRatio" in window.IntersectionObserverEntry.prototype) { | |
let lazyBackgroundObserver = new IntersectionObserver(function(entries, observer) { | |
entries.forEach(function(entry) { | |
if (entry.isIntersecting) { | |
entry.target.classList.add("bg-visible"); | |
lazyBackgroundObserver.unobserve(entry.target); | |
} | |
}); | |
}); | |
lazyBackgrounds.forEach(function(lazyBackground) { | |
lazyBackgroundObserver.observe(lazyBackground); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you got the enqueue line of code from the article on my website, you need to watch out with the quote symbols ( " and ' ) when you copy and paste. Just delete and type those characters in again. Something about the way the code renders on the site makes the encoding of those characters incorrect.
Hope this helps.