Skip to main content
Version: 3.x

useWheel

This hook provides a way to implement wheel event in any HTMLElement.

Arguments

callback [ function ]

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

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

PropertyDescription
targetCurrent target element to which the wheel event bound to.
deltaXWheel delta amount along horizontal axis.
deltaYWheel delta amount along vertical axis.
movementXAmount of movement in x-axis. Always starts from 0 while wheeling.
movementYAmount of movement in y-axis. Always starts from 0 while wheeling.
offsetXAmount of movement with offset in x-axis. It saves the previous movement and wheel starts from previous position.
offsetYAmount of movement with offset in y-axis. It saves the previous movement and wheel starts from previous position.
velocityXVelocity along horizontal wheel direction.
velocityYVelocity along vertical wheel direction.
directionXIndicates the current horizontal wheel direction. For positive +1, for negative -1 and for not wheeling 0.
directionYIndicates the current vertical wheel direction. For positive +1, for negative -1 and for not wheeling 0.

Returns

It returns a function which is spread on any HTMLELement.

Define a bind function:

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

Apply it on a HTMLELement:

<div {...bind()} style={{ width: 100, height: 100, backgroundColor: 'red' }} />

Example

In the below example, useWheel hook is used to capture wheel event on HTMLElement.

import { useAnimatedValue, useWheel, AnimatedBlock } from 'react-ui-animate';

export default function () {
const left = useAnimatedValue(0);

const bind = useWheel(function ({ movementX, isWheeling }) {
left.value = isWheeling ? movementX : 0;
});

return (
<AnimatedBlock
{...bind()} // bind here
style={{
width: 100,
height: 100,
backgroundColor: '#3399ff',
position: 'absolute',
left: left.value,
top: 0,
}}
/>
);
}