Skip to content

Instantly share code, notes, and snippets.

@ahsan-a
Created April 9, 2021 13:00
Show Gist options
  • Save ahsan-a/1b47b1477bcde45f2222aa8816cbedd3 to your computer and use it in GitHub Desktop.
Save ahsan-a/1b47b1477bcde45f2222aa8816cbedd3 to your computer and use it in GitHub Desktop.
Javascript scroll snapping every 100vh without CSS
<script>
window.addEventListener('scroll', (e) => {
let scrolled = window.pageYOffset;
setTimeout(() => {
if (scrolled == window.pageYOffset) {
if (
scrolled % window.innerHeight >=
window.innerHeight / 2
) {
window.scrollBy(
0,
window.innerHeight -
(scrolled % window.innerHeight),
);
} else {
window.scrollBy(
0,
-(scrolled % window.innerHeight),
);
}
}
}, 750);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment