Created
June 12, 2019 17:26
-
-
Save matticusfinch/f691fbcaa846a33110ac0bc1abe243c1 to your computer and use it in GitHub Desktop.
IntersectionObserver to use with Next-Gen Image Formats
This file contains hidden or 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
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