Skip to content

Instantly share code, notes, and snippets.

@celsowhite
Last active May 14, 2018 16:07
Show Gist options
  • Save celsowhite/772f8f10d34efd0695d550260636176c to your computer and use it in GitHub Desktop.
Save celsowhite/772f8f10d34efd0695d550260636176c to your computer and use it in GitHub Desktop.
Smooth scroll links. Uses event delegation.
/*----------------------------
SMOOTH SCROLL
----------------------------*/
$( "body" ).on( "click", ".smooth_scroll", function() {
// Breakpoint to adjust the offset distance.
const tabletLandscapeBreakpoint = window.matchMedia("(max-width: 1024px)");
// Get the target element & set normal offset
let target = $(this).attr('data-target');
let offsetDistance = -80;
// If below our breakpoint then adjust offset for a smaller mobile header
if (tabletLandscapeBreakpoint.matches) {
offsetDistance = -60;
}
target = $('#' + target);
$('html, body').animate({
scrollTop: target.offset().top + offsetDistance,
}, 1000);
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment