I am using the react-animate-on-scroll package based on the animate.css library for animating my text in web page. But I only want the animation to happen while scrolling down and not scrolling up. How can we restrict that?
<ScrollAnimation animateIn='fadeInUp' duration={0.7}>
<h1 className='mb-4'>Text with animation</h1>
</ScrollAnimation>
Since,
react-animate-on-scrolldoesn't directly provide the feature to adjust animation based on scrolling direction, unless this feature gets added, another method would be to use state variables and event listeners for scroll.In NPM, Node.js, utilize
window.pageYOffsetand calculatescrollDirectionas up or down based on the logicor in code
The component can make use of the useEffect hook for adding and removing the event listener.
window.scrollYis supported in most browsers. It represents the number of pixels that the document is currently scrolled vertically from the top.window.scrollTopis a safety check and for older browsers