Skip to content

Instantly share code, notes, and snippets.



Created Apr 10, 2019
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 =;
//check if hash is not empty
if(hash) {
// Scroll to that element
behavior: 'smooth'

This comment has been minimized.

Copy link

@webregie webregie commented Nov 19, 2020

Perhaps there is a problem with Mac and Safari... but thank you for this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment