Last active
June 15, 2024 05:05
-
-
Save Aslam97/de1c583934dd82ace0a3210ccca21def to your computer and use it in GitHub Desktop.
Check whether is scrolling to bot or top - React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [currentPosition, setCurrentPosition] = useState(0) | |
const [isScrollingToBottom, setIsScrollingToBottom] = useState(false) | |
useEffect(() => { | |
const handleScroll = () => { | |
const { scrollTop } = document.documentElement | |
const isScrollingDown = scrollTop > currentPosition | |
setIsScrollingToBottom(isScrollingDown) | |
setCurrentPosition(scrollTop) | |
} | |
window.addEventListener('scroll', handleScroll) | |
return () => { | |
window.removeEventListener('scroll', handleScroll) | |
} | |
}, [currentPosition]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment