Skip to content

Instantly share code, notes, and snippets.

@fiqryq
Created April 17, 2022 15:35
Show Gist options
  • Save fiqryq/3d7d1bf89abc525cf5cebdf762bbbc9f to your computer and use it in GitHub Desktop.
Save fiqryq/3d7d1bf89abc525cf5cebdf762bbbc9f to your computer and use it in GitHub Desktop.
detect scrool y offset.
const [scroll, serScroll] = useState(false);
const handleScroll = () => {
if (scroll !== (window.pageYOffset > 0)) {
serScroll(!scroll);
}
};
useEffect(() => {
window.addEventListener('scroll', handleScroll);
return () => {
window.removeEventListener('scroll', handleScroll);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment