Skip to content

Instantly share code, notes, and snippets.

@SrJSDev
Created April 15, 2023 23:49
Show Gist options
  • Save SrJSDev/ba76902a798ba5b7433e2e2cb377a6fc to your computer and use it in GitHub Desktop.
Save SrJSDev/ba76902a798ba5b7433e2e2cb377a6fc to your computer and use it in GitHub Desktop.
intersection observers with callback
export type Callback = (
entry: IntersectionObserverEntry,
observer: IntersectionObserver,
) => void
const opts = { threshold: 0 }
const observer = new IntersectionObserver(
(entries, ob) => {
entries.forEach((entry) => {
entry.target.classList.toggle("__is-intersecting", entry.isIntersecting)
map.get(entry.target)(entry, ob)
})
},
opts
)
const map = new WeakMap<Element, Callback>()
export function observeIntersection(el: Element, cb: Callback) {
map.set(el, cb)
observer.observe(el)
//console.log(el, cb)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment