Skip to content

Instantly share code, notes, and snippets.

@DefV
Forked from tijsverkoyen/scrollTo
Last active December 11, 2015 11:38
Show Gist options
  • Save DefV/4594724 to your computer and use it in GitHub Desktop.
Save DefV/4594724 to your computer and use it in GitHub Desktop.
$(document).on('click', 'a[href*="#"]', function(e) {
var $anchor = $(this),
hash = $anchor.attr('href'),
url = hash.substr(0, hash.indexOf('#')),
$hash = $(hash.substr(hash.indexOf('#')));
if(url.indexOf(document.location.pathname) >= 0 && $hash.length > 0)
{
e.preventDefault();
$('html, body').stop().animate({
scrollTop: $hash.offset().top
}, 1000);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment