Last active
August 24, 2019 09:16
-
-
Save lukaszmn/b8f27dec87130b4784fc7a6d3bfc400e to your computer and use it in GitHub Desktop.
Adds smooth scrolling to internal links with optional offset for top header - https://lukasznojek.com/blog/2019/08/automatically-enhance-headers-with-hierarchy-and-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
/** | |
* Adds smooth scrolling to internal links with optional offset for top header | |
* | |
* @param {number} offset - optional offset to add when scrolling | |
* @param {number} duration - the duration of the animation, by default 800 | |
* @param jQuery - reference to the jQuery | |
*/ | |
function smoothScrolling(offset = 0, duration = 800, jQuery = $) { | |
jQuery('a').click(function() { | |
jQuery('html, body').animate({ | |
scrollTop: jQuery( jQuery(this).attr('href') ).offset().top - offset | |
}, duration); | |
return false; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment