Skip to content

Instantly share code, notes, and snippets.

@lstanard
Last active August 11, 2022 16:12
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 lstanard/8107648 to your computer and use it in GitHub Desktop.
Save lstanard/8107648 to your computer and use it in GitHub Desktop.
Bind smooth scroll event to links
// Setup scrollto links
$('.scrollto').on('click', function(e) {
var targetElement = $('#' + $(this).attr('href').substring(1));
if (targetElement.length >= 1) {
var target = targetElement.offset().top,
scrollSpeed = function() { return Math.ceil( Math.abs( target - $w.scrollTop() ) / 1.5 ); };
$('html, body').animate({
scrollTop: target
}, scrollSpeed() );
}
e.preventDefault();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment