Skip to content

Instantly share code, notes, and snippets.

@generatepress
Last active October 5, 2016 07:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save generatepress/6e122667d28cabd23582 to your computer and use it in GitHub Desktop.
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
<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