Skip to content

Instantly share code, notes, and snippets.

@ShuvoHabib
Last active October 16, 2020 11:27
Show Gist options
  • Save ShuvoHabib/33a1e9658af04c2080a7f2c15bfc7ca8 to your computer and use it in GitHub Desktop.
Save ShuvoHabib/33a1e9658af04c2080a7f2c15bfc7ca8 to your computer and use it in GitHub Desktop.
Smooth Scroll jQuery and JS
$("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