Skip to content

Instantly share code, notes, and snippets.

@mcbrwr
Last active August 29, 2016 14:44
Show Gist options
  • Save mcbrwr/7fc6f4bba5f2c85c0dfa to your computer and use it in GitHub Desktop.
Save mcbrwr/7fc6f4bba5f2c85c0dfa to your computer and use it in GitHub Desktop.
jquery smooth scroll on all <a> tags
// smooth scroll
$('a[href*="#"]:not([href="#"])').on("click", function() {
if (location.pathname.replace(/^\//,'') === this.pathname.replace(/^\//,'') && location.hostname === this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
var toppie = target.offset().top - 50;
$('html,body').animate({
scrollTop: toppie
}, 600);
document.location.hash = this.hash.slice(1);
return false;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment