Created
April 9, 2021 13:00
-
-
Save ahsan-a/1b47b1477bcde45f2222aa8816cbedd3 to your computer and use it in GitHub Desktop.
Javascript scroll snapping every 100vh without CSS
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
<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