Skip to content

Instantly share code, notes, and snippets.

@drainpip
Last active December 16, 2015 10:58
Show Gist options
  • Select an option

  • Save drainpip/5423729 to your computer and use it in GitHub Desktop.

Select an option

Save drainpip/5423729 to your computer and use it in GitHub Desktop.
Assumes jQuery. Simply use class slide for any anchor to move to an ID, eg. <a href="#home" class="slide">Back to top</a>. Adjust duration for length of time. You could also do some math on the duration to make the speed constant based on distance. Remove "return false;" if you want to hashtag to show on the URL.
$(document).ready(function() {
$(".slide").click(function() {
$("html, body").animate({
scrollTop: $($(this).attr("href")).offset().top + "px"
}, {
duration: 500,
easing: "swing"
});
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment