Last active
September 10, 2023 08:52
-
-
Save yansusanto/3ac0860da0628ae163b65b93e036a2a9 to your computer and use it in GitHub Desktop.
On scroll effect based on the percentage of viewport height
This file contains 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 [inView, setInView] = React.useState(false); | |
React.useEffect(() => { | |
const handleScroll = () => { | |
const scrollThresholdVH = 30; | |
if (window.scrollY > (window.innerHeight * scrollThresholdVH) / 100) { | |
setInView(true); | |
} else { | |
setInView(false); | |
} | |
}; | |
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