Skip to content

Instantly share code, notes, and snippets.

@matticusfinch
Created June 12, 2019 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matticusfinch/f691fbcaa846a33110ac0bc1abe243c1 to your computer and use it in GitHub Desktop.
Save matticusfinch/f691fbcaa846a33110ac0bc1abe243c1 to your computer and use it in GitHub Desktop.
IntersectionObserver to use with Next-Gen Image Formats
const options = {
rootMargin: "25px",
threshold: 0.1
};
const io = new IntersectionObserver(entries => {
entries.forEach(entry => {
//console.log('entry: ', entry);
if (entry.intersectionRatio > 0.1) {
var imgsrc = entry.target.getAttribute("srcset1");
entry.target.setAttribute("srcset", imgsrc);
entry.target.removeAttribute("srcset1");
io.unobserve(entry.target);
}
});
}, options);
const targetElements = document.querySelectorAll("source");
for (let element of targetElements) {
//console.log('element: ', element);
io.observe(element);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment