Last active
August 11, 2022 16:12
-
-
Save lstanard/8107648 to your computer and use it in GitHub Desktop.
Bind smooth scroll event to links
This file contains 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
// 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