useWindowScroll

Track and manipulate the scroll position of a web page with useWindowScroll.

Install:

npm i @uidotdev/usehooks

Description:

The useWindowScroll hook is useful for tracking and manipulating the scroll position of a web page within a React component. It allows you to easily access the current scroll coordinates (x and y) through the “state” object returned by the hook. Additionally, the hook provides a “scrollTo” function that can be used to programmatically scroll to a specific position on the page. This hook is helpful when building components that require dynamic behavior based on the scroll position, such as lazy-loading content, infinite scrolling, or implementing smooth scrolling animations.

Return Value

The useWindowScroll hook returns an array with two elements:

NameTypeDescription
stateobjectAn object representing the current window scroll position.
scrollTofunctionA function that can be used to scroll the window to a specific position.

Demo:

Example:

More Hooks:

Sort: