Skip to content

Instantly share code, notes, and snippets.

@Jonsey
Created January 18, 2019 10:55
Show Gist options
  • Save Jonsey/03178640cd3124fef6828334e3e00b9c to your computer and use it in GitHub Desktop.
Save Jonsey/03178640cd3124fef6828334e3e00b9c to your computer and use it in GitHub Desktop.
Using the intersection observer with Elm
function onEntry(entry) {
entry.forEach((change) => {
if(change.isIntersecting) {
change.target.classList.add('visible');
}
});
}
const intersect = () => {
let options = {
threshold: [1]
};
let observer = new IntersectionObserver(onEntry, options);
let elements = document.getElementsByClassName('lazy');
for (let elm of elements) {
observer.observe(elm);
}
}
(function() {
window.addEventListener("load", intersect);
if (window.history && window.history.pushState) {
window.addEventListener('popstate', function() {
window.setTimeout(intersect, 100);
});
}
})();
ports.urlChange.subscribe(url => {
intersect();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment