Skip to content

Instantly share code, notes, and snippets.

@fijiwebdesign
Last active August 29, 2015 14:06
Show Gist options
  • Save fijiwebdesign/e861808b9d57012eb795 to your computer and use it in GitHub Desktop.
Save fijiwebdesign/e861808b9d57012eb795 to your computer and use it in GitHub Desktop.
Make all in page anchors scroll page with animation
// requires jquery selector
$("a[href^=#]").click(function(e) {
var duration = $(this).data('scroll') || 750;
var target = $(this).attr('href');
var $el = $(target);
if ($el.length || target == '#') {
e.preventDefault(); // prevent jumping to target or adding hash in url
// scroll to the element
$('html, body').animate({
scrollTop: target == '#' ? 0 : $el.offset().top
}, duration, function() {
location.hash = target; // add the hash to url
});
}
});
@fijiwebdesign
Copy link
Author

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