useScroll
Tracks scroll position and provides scroll-related info about the page.
Installation
npx hookcn add useScrollDescription
The useScroll hook monitors the scroll position of the window and provides detailed information about it, including:
Current scroll position (
scrollY)Scroll progress in percentage (
scrollPercent)Whether the page is scrolled to the top or bottom
A helper method to scroll back to the top smoothly
This is useful for triggering animations, showing scroll progress indicators, or rendering "back to top" buttons.
Usage
import useScroll from "src/hooks/useScroll";
//...
const { scrollY, scrollPercent, isAtTop, isAtBottom, scrollToTop } = useScroll();
};Return Value
scrollY
number
Vertical scroll position in pixels.
scrollPercent
number
Percentage of the document that has been scrolled.
isAtTop
boolean
Whether the window is at the top of the page.
isAtBottom
boolean
Whether the window is at the bottom of the page.
scrollToTop
() => void
Smoothly scrolls the window back to the top.
Example
Last updated