Last active
October 16, 2020 11:27
-
-
Save ShuvoHabib/33a1e9658af04c2080a7f2c15bfc7ca8 to your computer and use it in GitHub Desktop.
Smooth Scroll jQuery and JS
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
$("a").on('click', function(event) { | |
if (this.hash !== "") { | |
event.preventDefault(); | |
var hash = this.hash; | |
$('html, body').animate({ | |
scrollTop: $(hash).offset().top | |
}, 800, function(){ | |
window.location.hash = hash; | |
}); | |
} | |
}); | |
const links = document.querySelectorAll(".page-header ul a"); | |
for (const link of links) { | |
link.addEventListener("click", clickHandler); | |
} | |
function clickHandler(e) { | |
e.preventDefault(); | |
const href = this.getAttribute("href"); | |
const offsetTop = document.querySelector(href).offsetTop; | |
scroll({ | |
top: offsetTop, | |
behavior: "smooth" | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment