Overview
Observes intersection changes for the attached element and stores the latest entry. Recreates the observer when options change and clears the entry when no element is attached.Function Signature
Type Definitions
Parameters
Optional
IntersectionObserver configuration:root: Element used as viewport for checking visibility (defaults to browser viewport)rootMargin: Margin around the root (e.g.,"10px 20px 30px 40px")threshold: Number or array indicating at what percentage of visibility the callback should execute (e.g.,0.5for 50%, or[0, 0.25, 0.5, 0.75, 1])
Return Value
A ref callback to attach to the element you want to observe.
The latest intersection observer entry containing:
isIntersecting: Boolean indicating if element is intersectingintersectionRatio: Ratio of element visibility (0 to 1)boundingClientRect: Element’s bounding rectangleintersectionRect: Rectangle of the intersectionrootBounds: Root element’s bounding rectangletarget: The observed elementtime: Timestamp when intersection occurred
Usage Example
Lazy Loading Example
Root Margin Example
Features
- Uses native
IntersectionObserverAPI for efficient viewport detection - Automatic observer cleanup when element is removed
- Recreates observer when options change
- Provides full
IntersectionObserverEntrywith detailed intersection data - Supports custom root elements and margins
- Configurable visibility thresholds