Skip to content

Instantly share code, notes, and snippets.

@yansusanto
Last active September 10, 2023 08:52
Show Gist options
  • Save yansusanto/3ac0860da0628ae163b65b93e036a2a9 to your computer and use it in GitHub Desktop.
Save yansusanto/3ac0860da0628ae163b65b93e036a2a9 to your computer and use it in GitHub Desktop.
On scroll effect based on the percentage of viewport height
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