Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cameronjonesweb
Last active September 27, 2022 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cameronjonesweb/4a90312f470100925610a33ebf48b807 to your computer and use it in GitHub Desktop.
Save cameronjonesweb/4a90312f470100925610a33ebf48b807 to your computer and use it in GitHub Desktop.
jQuery Scroll To Anchor
function scrollToAnchor( $headers ) {
jQuery( 'a[href^="#"]' ).each( function() {
if ( jQuery( this ).attr( 'href' ).length > 1 ) {
if ( jQuery( 'body' ).find( jQuery( this ).attr( 'href' ) ) ) {
jQuery( this ).click( function( e ){
e.preventDefault();
$offset = 0;
for ( i = 0; i < $headers.length; i++ ) {
if ( jQuery( $headers[ i ] ).length ) {
$offset += jQuery( $headers[ i ] ).outerHeight();
}
}
jQuery( 'html, body' ).animate({
scrollTop: jQuery( jQuery( this ).attr( 'href' ) ).offset().top - $offset
}, 'slow' );
});
}
}
});
}
jQuery( document ).ready( function() {
scrollToAnchor( [ '.site-header', '#wpadminbar' ] );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment