Skip to content

Instantly share code, notes, and snippets.

@GiuMagnani
Last active May 5, 2019 10:53
Show Gist options
  • Save GiuMagnani/e85e552dd2374d0f4cf3b278dd12e1aa to your computer and use it in GitHub Desktop.
Save GiuMagnani/e85e552dd2374d0f4cf3b278dd12e1aa to your computer and use it in GitHub Desktop.
jQuery(document).ready(function() {
jQuery('a').on('click', function(e) {
smooth_scroll_to(this.hash, e);
});
});
jQuery(window).on('load',function() {
if(window.location.hash) {
smooth_scroll_to(window.location.hash);
}
});
function smooth_scroll_to(hash, e) {
if(hash === '') {
return false;
} else {
if(jQuery(hash).length > 0) {
if(typeof e !== 'undefined') {
e.preventDefault();
history.pushState(null, null, jQuery(e.target).attr('href'));
}
jQuery('html, body').animate({
scrollTop: jQuery(hash).offset().top
}, 1500, 'swing');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment