Skip to content

Instantly share code, notes, and snippets.

@Heydon
Last active December 18, 2020 11:52
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Heydon/919dd58cc732d11fee67decc2edff73a to your computer and use it in GitHub Desktop.
Save Heydon/919dd58cc732d11fee67decc2edff73a to your computer and use it in GitHub Desktop.
// Elements with `data-observe` toggle `data-visible`
// between `true` and `false`
if ('IntersectionObserver' in window) {
const callback = (entries, observer) => {
entries.forEach(entry => {
entry.target.setAttribute('data-visible', entry.isIntersecting)
})
}
const observer = new IntersectionObserver(callback)
const targets = document.querySelectorAll('[data-observe]')
targets.forEach(t => observer.observe(t))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment