Skip to content

Instantly share code, notes, and snippets.

@mul14
Created September 10, 2016 23:29
Show Gist options
  • Save mul14/bddd1fcfd9712f47fc765c4b52be6adf to your computer and use it in GitHub Desktop.
Save mul14/bddd1fcfd9712f47fc765c4b52be6adf to your computer and use it in GitHub Desktop.
Smooth animated scrolling with JavaScript
// URL: https://css-tricks.com/snippets/jquery/smooth-scrolling/#comment-1599892
$(document).on('click', 'a[href*="#"]:not([href="#"])', function(e) {
if (location.pathname.replace(/^\//,'') === this.pathname.replace(/^\//,'') && location.hostname === this.hostname) {
let target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
e.preventDefault();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment