Skip to content

Instantly share code, notes, and snippets.

@townivan
Created June 15, 2016 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save townivan/3be437575b70acbb8ce9e2a966aa33d6 to your computer and use it in GitHub Desktop.
Save townivan/3be437575b70acbb8ce9e2a966aa33d6 to your computer and use it in GitHub Desktop.
// source: https://jonsuh.com/blog/better-scroll-to-anchor-links/
$(".anchorLink").click(function(e){
e.preventDefault();
var this_offset = $(this).offset();
var that_id = $(this).attr("href");
var that_offset = $(that_id).offset();
var offset_diff = Math.abs(that_offset.top - this_offset.top);
var base_speed = 100; // Time in ms per 1,000 pixels
var speed = (offset_diff * base_speed) / 1000;
$("html,body").animate({
scrollTop: that_offset.top
}, speed);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment