Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Close the mobile menu on item click + smooth scroll to ID on page - add into wp_footer hook
<script>
jQuery( document ).ready( function( $ ) {
$( document ).on( 'click', '.main-nav li > a:first-child', function( e ) {
var mobile = $( '.menu-toggle' );
if ( mobile.is( ':visible' ) ) {
$( '.menu-toggle').closest( '.main-navigation' ).removeClass( 'toggled' );
$( '.menu-toggle').closest( '.main-navigation' ).attr( 'aria-expanded', $( '.menu-toggle').closest( '.main-navigation' ).attr( 'aria-expanded' ) === 'true' ? 'false' : 'true' );
$( '.menu-toggle').removeClass( 'toggled' );
$( '.menu-toggle').children( 'i' ).addClass( 'fa-bars' ).removeClass( 'fa-close' );
$( '.menu-toggle').attr( 'aria-expanded', $( this ).attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
}
var target = $(this).prop("hash");
if (target && typeof $( target ).offset().top != 'undefined' ) {
$target = $( target );
$height = $( '.main-navigation' ).outerHeight();
$('html, body').stop().animate({
'scrollTop': $target.offset().top - $height * 2
}, 900, 'swing', function () {
window.location.hash = target;
});
}
});
$( document ).on( 'click', '.entry-content a', function( e ) {
var target = $(this).prop("hash");
if (target) {
e.preventDefault();
$target = $( target );
$height = $( '.main-navigation' ).outerHeight();
$('html, body').stop().animate({
'scrollTop': $target.offset().top - $height * 2
}, 900, 'swing', function () {
window.location.hash = target;
});
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment