shadcn-ahooks

useScroll

Get the scroll position of an element.

Overview

Get the scroll position of an element.

Documentation and Examples

Installation

Open in
pnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useScroll.json
npx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useScroll.json
yarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useScroll.json
bun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useScroll.json

Get the scroll position of an element.

Examples

Basic Usage

Detect Whole Page Scroll

Control listen on scroll status

API

const position = useScroll(target, shouldUpdate);

Params

PropertyDescriptionTypeDefault
targetDOM element or ref objectElement | Document | (() => Element) | MutableRefObject<Element>document
shouldUpdateWhether update position({ top: number, left: number }) => boolean() => true

Result

PropertyDescriptionType
positionThe current scroll position of the element.{ left: number, top: number } | undefined

On this page