[Resolved] Auto Collapse Mobile Menu

Home Forums Support [Resolved] Auto Collapse Mobile Menu

Home Forums Support Auto Collapse Mobile Menu

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #228987
    Alex

    Iโ€™m working with a single-page site that uses section idโ€™s for the nav menu links. When viewing on a mobile screen, the mobile menu stays open after clicking a link. I would like to have the mobile menu automatically collapse when a menu item is clicked. Is there a theme option for this that I am missing, or will this need to be accomplished by adding a jquery script?

    #229015
    Tom
    Lead Developer
    Lead Developer

    Hi Alex,

    This code might help: https://gist.github.com/generatepress/6e122667d28cabd23582

    Let me know ๐Ÿ™‚

    #229038
    Alex

    Hi Tom,

    Yes, works perfectly! thank you ๐Ÿ™‚

    #229094
    Tom
    Lead Developer
    Lead Developer

    You’re welcome ๐Ÿ™‚

    #289625
    Steve

    Hello – i tried to strip the jquery off the smooth scroll ability since iam using “page scroll 2 id”.
    It is working like intended but I never used jquerys before so would you mind taking a look? (i fear that i added bugs or take infinite ram or something bad)

    <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' );
    		}
        });
    });
    </script>
    #289723
    Leo
    Staff
    Customer Support

    Hi Steve,

    Looks like the same block of code as Tom provided here: https://generatepress.com/forums/topic/responsive-menu-and-anchor-links/#post-273558 so it should be good!

    #289725
    Steve

    Oh i didnt found that.
    Thank you very much.

    #289728
    Leo
    Staff
    Customer Support

    You’re welcome ๐Ÿ™‚

    #289806
    Tom
    Lead Developer
    Lead Developer

    The next version of GP does this for you automatically, so you’ll be able to remove that JS.

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.