Skip to main content
Version: 3.x

useScroll

This hook provides a way to handle scroll event in any HTMLElement or window.

Arguments

callback [ function ]

First argument is a callback function with event object as its first argument which is called initially and is called on every scroll.

Here are the properties of an event object argument of a callback function:

PropertyDescription
isScrollingBoolean indicating the current status of scrolling.
scrollXHorizontal scroll amount.
scrollYVertical scroll amount.
velocityXVelocity along horizontal scrolling direction.
velocityYVelocity along vertical scrolling direction.
directionXIndicates the current horizontal scrolling direction. For positive +1, for negative -1 and for not scrolling 0.
directionYIndicates the current vertical scrolling direction. For positive +1, for negative -1 and for not scrolling 0.

Returns

It returns a function which can be spread on any HTMLELement. If not bound to any HTMLElement, event is attached to window.

Define a hook for window:

useScroll((event) => doSomething(event));

Or you could bind it in any HTMLElement:

const bind = useScroll((event) => doSomething(event));

Apply it on a HTMLELement:

<div {...bind()}>...</div>

Example

In the below example, useScroll hook is used to get scrollY.

import { useScroll } from "react-ui-animate";

export default function() {

const bind = useScroll(
function ({ scrollY }) {
console.log("Vertical scrolling amount", scrollY);
},
);

return (...);
}