observeIntersection
Calls callback each time element enters or leaves the viewport (or options.root), with an
IntersectionObserver dedicated to it: lazy loading, starting an animation when it becomes
visible, pausing a video when it scrolls away. Like IntersectionObserver itself, it is also
called once soon after it starts observing.
Without IntersectionObserver, it does nothing and returns a function that does nothing.
Available since v3.1.5
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
element | Element | The element to observe |
callback | function | Receives the `IntersectionObserverEntry` of `element` (`entry.isIntersecting`, `entry.intersectionRatio`, …) |
options | ObserveIntersectionOptions | `root`, `rootMargin` and `threshold`, as for `IntersectionObserver`, and a `signal` to stop — see ObserveIntersectionOptions |
Returns
Section titled “Returns”function — A function that stops observing
Examples
Section titled “Examples”Play a video only while visible
Section titled “Play a video only while visible”The callback receives the IntersectionObserverEntry of the element.
Lazy-load once
Section titled “Lazy-load once”Stop observing from the callback after the first load; rootMargin starts loading early.
Related Types
Section titled “Related Types”ObserveIntersectionOptions
Section titled “ObserveIntersectionOptions”Options for observeIntersection: those of IntersectionObserver, and a signal.
