Last active
September 27, 2022 15:56
-
-
Save cameronjonesweb/4a90312f470100925610a33ebf48b807 to your computer and use it in GitHub Desktop.
jQuery Scroll To Anchor
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
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' ); | |
}); | |
} | |
} | |
}); | |
} |
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
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