Created
August 27, 2017 18:22
-
-
Save matryer/b0e6c5b895429267221e91788ebd09b3 to your computer and use it in GitHub Desktop.
Automatic gentle scroll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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