useInViewport
Observe whether the element is in the visible area, and the visible area ratio of the element. More information refer to [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
Overview
Observe whether the element is in the visible area, and the visible area ratio of the element. More information refer to Intersection Observer API.
Installation
Open inpnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useInViewport.jsonnpx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useInViewport.jsonyarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useInViewport.jsonbun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useInViewport.jsonObserve whether the element is in the visible area, and the visible area ratio of the element. More information refer to Intersection Observer API.
Examples
Default usage
Observe the visible area ratio of element
Listening content scrolling selection menu
API
type Target = Element | (() => Element) | React.MutableRefObject<Element>;
const [inViewport, ratio] = useInViewport(
target: Target | Target[],
options?: Options
);Params
| Property | Description | Type | Default |
|---|---|---|---|
| inViewport | Is visible | boolean | undefined | |
| ratio | Current visible ratio, updated every time the node set by options.threshold is reached | number | undefined |