Last active
August 29, 2016 14:44
-
-
Save mcbrwr/7fc6f4bba5f2c85c0dfa to your computer and use it in GitHub Desktop.
jquery smooth scroll on all <a> tags
This file contains 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
// 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