Skip to content

useScrollPosition

This hook tracks the scroll position and progress of a target element.

Parameters

  • the ref object of the target element (default is window).

Return Values

NameTypeDescription
refRefObjectthe target element reference.
xNumberthe x scroll position.
yNumberthe y scroll position.
progressXNumberthe x scroll progress (between 0 and 1).
progressYNumberthe y scroll progress (between 0 and 1).

Example Usage

tsx
import { useScrollPosition } from 'react-pre-hooks';

export default function Example() {
  const { x, progressY } = useScrollPosition();

  return (
    <main>
      <p>
        x: {x}px, y: {progressY}%
      </p>
    </main>
  );
}
import { useScrollPosition } from 'react-pre-hooks';

export default function Example() {
  const { x, progressY } = useScrollPosition();

  return (
    <main>
      <p>
        x: {x}px, y: {progressY}%
      </p>
    </main>
  );
}

Released under the MIT License.