Created
April 15, 2023 23:49
-
-
Save SrJSDev/ba76902a798ba5b7433e2e2cb377a6fc to your computer and use it in GitHub Desktop.
intersection observers with callback
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
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