Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active September 18, 2021 21:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save billerickson/804efbdee63a0a113d12 to your computer and use it in GitHub Desktop.
Save billerickson/804efbdee63a0a113d12 to your computer and use it in GitHub Desktop.
// Smooth scrolling anchor links
function ea_scroll( hash ) {
var target = $( hash );
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
var top_offset = 0;
if ( $('.site-header').css('position') == 'fixed' ) {
top_offset = $('.site-header').height();
}
if( $('body').hasClass('admin-bar') ) {
top_offset = top_offset + $('#wpadminbar').height();
}
$('html,body').animate({
scrollTop: target.offset().top - top_offset
}, 1000);
return false;
}
}
// -- Smooth scroll on pageload
if( window.location.hash ) {
ea_scroll( window.location.hash );
}
// -- Smooth scroll on click
$('a[href*="#"]:not([href="#"]):not(.no-scroll)').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') || location.hostname == this.hostname) {
ea_scroll( this.hash );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment