Skip to content

Instantly share code, notes, and snippets.

@matryer
Created August 27, 2017 18:22
Show Gist options
  • Save matryer/b0e6c5b895429267221e91788ebd09b3 to your computer and use it in GitHub Desktop.
Save matryer/b0e6c5b895429267221e91788ebd09b3 to your computer and use it in GitHub Desktop.
Automatic gentle scroll
// gentle scrolling
// Anything with an href that points to something on the page
// is gently scrolled to, rather than jumping.
// Everything else is left alone.
$("a[href]").click(function(e) {
var dest = $(this).attr('href')
dest = dest.substr(dest.indexOf('#')+1)
var destEl = $('[id="'+dest+'"]')
if (destEl.length > 0) {
e.preventDefault()
$('html,body').animate({
scrollTop: destEl.offset().top
}, 'slow')
if (history.pushState) {
history.pushState(null, null, '#'+dest);
} else {
location.hash = '#'+dest;
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment