Last active
October 5, 2016 07:10
-
-
Save generatepress/6e122667d28cabd23582 to your computer and use it in GitHub Desktop.
Close the mobile menu on item click + smooth scroll to ID on page - add into wp_footer hook
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
<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