Skip to content

Instantly share code, notes, and snippets.

@raddevon
Created June 19, 2013 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raddevon/5815372 to your computer and use it in GitHub Desktop.
Save raddevon/5815372 to your computer and use it in GitHub Desktop.
Smooth scrolling anchor links with jQuery. This script was taken from http://www.paulund.co.uk/smooth-scroll-to-internal-links-with-jquery . I modified it to fix a bug when using it with anchor links pointing to the top of the page ('#').
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': ($target.offset()) ? $target.offset().top : 0
}, 400, 'ease', function () { // Set the duration and easing function as desired.
window.location.hash = target;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment