Created
January 18, 2019 10:55
-
-
Save Jonsey/03178640cd3124fef6828334e3e00b9c to your computer and use it in GitHub Desktop.
Using the intersection observer with Elm
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
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