Skip to content

Instantly share code, notes, and snippets.

@erickjones
Last active August 29, 2015 14:07
Show Gist options
  • Save erickjones/919d78198511783d59bc to your computer and use it in GitHub Desktop.
Save erickjones/919d78198511783d59bc to your computer and use it in GitHub Desktop.
Scrolling to Anchors with jQuery
$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 400, 'swing', function () {
window.location.hash = target;
});
});
});
@erickjones
Copy link
Author

Get all anchors (#) in the page and scroll to its respective id wit: 400ms and an ease 'swing'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment