Created
August 1, 2020 09:53
-
-
Save nicolebeaumont/301ec0fdde4a3635c45698acc9514cbc to your computer and use it in GitHub Desktop.
Animated smooth scroll plain js, simple
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
// attach animated scroll to all hash links | |
document.querySelectorAll("a[href^='#']").forEach(element => { | |
const [, id] = element.href.split('#') | |
element.addEventListener("click", e => { | |
e.preventDefault() | |
document | |
.getElementById(id) | |
.scrollIntoView({behavior: 'smooth'}); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment