Skip to content

Instantly share code, notes, and snippets.

@lukaszmn
Last active August 24, 2019 09:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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/
/**
* 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