Overview
TheinView() function detects when elements enter and leave the viewport. It provides a simple callback-based API built on the Intersection Observer API.
Import
Signature
Parameters
elementOrSelector (required)
The element(s) to observe. Can be a single element, array of elements, or a CSS selector.onStart (required)
Callback function that fires when the element enters the viewport.root
The scrolling container. Defaults to the viewport.margin
Margin around the root element. Similar to CSS margin."0px""0px 100px""0px 100px -50px 0px"
amount
How much of the element must be visible."some"(default): Any part visible triggers"all": Entire element must be visiblenumber: Percentage from 0 to 1
Return Value
Returns a cleanup function to stop observing.Examples
Basic usage
Enter and exit animations
Trigger once
Stagger multiple elements
With animation library
Lazy load images
Video autoplay
Scroll-triggered counter
Container scrolling
Track analytics
Progressive disclosure
IntersectionObserverEntry
The entry object passed to callbacks contains:Notes
- Uses native Intersection Observer API
- Automatically handles cleanup when elements are removed
- Observes all matching elements when using a selector
- More performant than scroll event listeners
- Well supported in modern browsers