Last active
July 25, 2020 04:15
-
-
Save yansusanto/0282e59ea0cee4a32860bc373ee9172e to your computer and use it in GitHub Desktop.
Smooth scrolling in Vanilla JS
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
document.querySelectorAll('a[href^="#"]').forEach((anchor) => { | |
anchor.addEventListener("click", (e) => { | |
e.preventDefault(); | |
document.querySelector(anchor.getAttribute("href")) | |
.scrollIntoView({ | |
behavior: "smooth", | |
block: "start", | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment