Skip to content

Instantly share code, notes, and snippets.

@hsnyc hsnyc/scrollme.js
Created Apr 10, 2019

Embed
What would you like to do?
Smooth scroll to #hash links
// Smooth Scroll to #links ============================================= //
//get all # links in the document
var links = document.querySelectorAll('a[href*="#"]');
//assign a click event to all the # links
for(var l = 0; l < links.length; l++) {
links[l].addEventListener('click', scrollMe, false);
}
function scrollMe(e) {
e.preventDefault(); //needed in order for the scroll to work
var hash = e.target.hash;
//check if hash is not empty
if(hash) {
// Scroll to that element
document.querySelector(hash).scrollIntoView({
behavior: 'smooth'
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.