useScroll
The useScroll hook captures scroll activity on any DOM element or the window. It provides detailed scroll information including position, velocity, and movement deltas - perfect for scroll-triggered animations, parallax effects, and progress indicators.
Why Use useScroll?
useScroll simplifies scroll interactions by:
- Tracking scroll position - Know exactly where users have scrolled
- Providing velocity - Detect scroll speed for momentum effects
- Calculating deltas - Track scroll changes between frames
- Multiple targets - Monitor scroll on multiple elements
- Cross-browser support - Consistent behavior across browsers
Basic Syntax
useScroll(
refs: RefObject<HTMLElement> | RefObject<HTMLElement>[] | Window,
callback: (event: ScrollEvent & { index: number }) => void
): void
Parameters
- refs (required): Element(s) or
windowto monitor for scroll - callback (required): Function called on scroll with event data