Skip to content

Instantly share code, notes, and snippets.

@Aslam97
Last active June 15, 2024 05:05
Show Gist options
  • Save Aslam97/de1c583934dd82ace0a3210ccca21def to your computer and use it in GitHub Desktop.
Save Aslam97/de1c583934dd82ace0a3210ccca21def to your computer and use it in GitHub Desktop.
Check whether is scrolling to bot or top - React
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