Skip to content

Instantly share code, notes, and snippets.

@andyError
Last active July 26, 2017 15:42
Show Gist options
  • Save andyError/1acbd9e9b3004d6ace27 to your computer and use it in GitHub Desktop.
Save andyError/1acbd9e9b3004d6ace27 to your computer and use it in GitHub Desktop.
Scroll smoothly to #hash
$('nav a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
var distanceBetween = $target.offset().top - $(document).scrollTop();
var animationLength = Math.abs(distanceBetween * 0.25);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, animationLength, 'swing', function () {
window.location.hash = target;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment