Detect scroll direction on React
Hello all,
A quick snippet that can prove to be useful:
<div onWheel={ event => {
if (event.nativeEvent.wheelDelta > 0) {
console.log('scroll up');
} else {
console.log('scroll down');
}
}}
>
scroll on me!
</div>
onWheel synthetic event returns an event object having an attribute named nativeEvent
containing the original event information. wheelDelta
is used to detect the direction even if there is no effective scroll (overflow:hidden
).
Thanks for this. Been struggling for a while to find a decent way to implement greater functionality on scroll event listener on something other than the entire document in React 🙂
Thanks, you helped me a lot!
I love you!!
Bless you
I love you! thanks
Not working on mobile
awesome man…. this works like a charm…❤️
Thanks a lot, you save me