Skip to content

Instantly share code, notes, and snippets.

@freshyill
Created January 16, 2018 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freshyill/1b6dcc69a144018cc8ccf65848347092 to your computer and use it in GitHub Desktop.
Save freshyill/1b6dcc69a144018cc8ccf65848347092 to your computer and use it in GitHub Desktop.
Simple smooth scrolling for internal links
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