Skip to content

Instantly share code, notes, and snippets.

@nicolebeaumont
Created August 1, 2020 09:53
Show Gist options
  • Save nicolebeaumont/301ec0fdde4a3635c45698acc9514cbc to your computer and use it in GitHub Desktop.
Save nicolebeaumont/301ec0fdde4a3635c45698acc9514cbc to your computer and use it in GitHub Desktop.
Animated smooth scroll plain js, simple
// 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