Skip to content

Instantly share code, notes, and snippets.

@luisfelipe-dev
Last active September 9, 2020 13:16
Show Gist options
  • Save luisfelipe-dev/fc2d0a0ad7d698cdd7213329beab08aa to your computer and use it in GitHub Desktop.
Save luisfelipe-dev/fc2d0a0ad7d698cdd7213329beab08aa to your computer and use it in GitHub Desktop.
const headerElement = useRef(null);
useEffect(() => {
window.onscroll = () => {
const headerScrollPosition = headerElement.current.scrollTop + headerElement.current.clientHeight;
if (window.scrollY >= headerScrollPosition) {
setScrolled(true);
} else {
setScrolled(false);
}
}
return () => {
window.onscroll = () => {};
}
}, [
open,
scrolled
]);
<Style.Header ref={ headerElement } className={ scrolled ? 'app-fixed-top' : '' }>
position: fixed;
top: 0;
left: 0;
right: 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment