Created
January 16, 2018 05:47
-
-
Save freshyill/1b6dcc69a144018cc8ccf65848347092 to your computer and use it in GitHub Desktop.
Simple smooth scrolling for internal links
This file contains hidden or 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
function smoothScroll() { | |
const internalLinks = document.querySelectorAll('a[href^="#"]'); | |
internalLinks.forEach( | |
function(currentLink) { | |
const linkHref = currentLink.getAttribute('href'); | |
currentLink.addEventListener("click", function(event) { | |
document.querySelector(linkHref).scrollIntoView({ | |
behavior: 'smooth' | |
}); | |
event.preventDefault(); | |
}, false); | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment