Skip to Content
ohooks

useElementScroll

Tracks a scrollable element's scroll position, active scrolling state, movement directions, and edge arrival flags.

Loading preview...

The useElementScroll hook monitors a scrollable element's x and y scroll offsets, active scrolling state, movement directions (left, right, top, bottom), and edge arrival flags (arrived), while providing a programmatic scrollTo helper.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useElementScroll.json

Usage

Examples

Infinite Scroll Container

Check arrived.bottom together with an offset.bottom threshold to load more items when the user nears the bottom of a list.

Scroll-to-Top Button inside a Container

Use y to show a button after scrolling down and scrollTo to smoothly return to the top.

API Reference

UseElementScrollOptions

Configuration options for scroll tracking, offsets, and scroll behavior.

Property
Specification
throttleMs?
numberdefault:0
Throttle interval in milliseconds for scroll updates
idleDelayMs?
numberdefault:150
Delay in milliseconds after the last scroll event before isScrolling resets to false
offset?
object
Edge arrival offset thresholds in pixels

UseElementScrollReturn

Scroll coordinates, direction flags, arrival state, and control helpers returned by the hook.

Property
Specification
x
number
Current horizontal scroll offset (scrollLeft)
y
number
Current vertical scroll offset (scrollTop)
isScrolling
union
Whether the element is actively scrolling
directions
object
Active scroll movement directions
arrived
object
Flags indicating whether the scroll position has reached any edge
scrollTo
function
Programmatically scrolls the target element to given coordinates or ScrollToOptions

ScrollDirections

Flags indicating the direction of the most recent scroll movement.

Property
Specification
leftrequired
union
Whether the element is currently scrolling left
rightrequired
union
Whether the element is currently scrolling right
toprequired
union
Whether the element is currently scrolling up
bottomrequired
union
Whether the element is currently scrolling down

ScrollArrivedState

Flags indicating whether the scroll container has reached any of its edges.

Property
Specification
top
union
Whether the scroll position has reached the top edge
bottom
union
Whether the scroll position has reached the bottom edge
left
union
Whether the scroll position has reached the left edge
right
union
Whether the scroll position has reached the right edge

ScrollOffset

Pixel thresholds from each edge used when computing arrived flags.

Property
Specification
top?
numberdefault:0
Threshold in pixels from the top edge to consider arrived.top as true
bottom?
numberdefault:0
Threshold in pixels from the bottom edge to consider arrived.bottom as true
left?
numberdefault:0
Threshold in pixels from the left edge to consider arrived.left as true
right?
numberdefault:0
Threshold in pixels from the right edge to consider arrived.right as true