[Resolved] Auto close mobile menu when scrolling

Home Forums Support [Resolved] Auto close mobile menu when scrolling

Home Forums Support Auto close mobile menu when scrolling

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1665709
    Nick

    Hi team –

    Is there a way to autoclose the mobile menu when you start to scroll on a page?

    To demonstrate, load the page below on a mobile device then click the hamburger icon. If you then start to try and scroll on the page, I would like the menu to auto-close (in the same way as when you click the X button).

    thanks, Nick

    #1665954
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    This could open a can of worms, but in its simplest form you should be able to do something like this:

    document.addEventListener( 'scroll', function( e ) {
        var openedMenus = document.querySelectorAll( '.toggled .menu-toggle' );
    
        for ( var toggle = 0; toggle < openedMenus.length; toggle++ ) {
            openedMenus[ toggle ].addEventListener( 'click', function() {
                openedMenus[ toggle ].click();
            } );
        }
    } );
    #1665972
    Nick

    thanks Tom – I added this however it doesn’t appear to do anything

    #1666794
    Tom
    Lead Developer
    Lead Developer
    #1666820
    Nick

    Have updated the code, still doesn’t seem to do anything

    #1666822
    Tom
    Lead Developer
    Lead Developer

    Sorry about that – made one more edit. Hopefully this is the one ๐Ÿ™‚

    #1666830
    Nick

    just updated and cleared caches, still doesn’t seem to be doing anything

    #1668072
    Tom
    Lead Developer
    Lead Developer

    Can you deactivate your caching/minifying so I can view the source of your site cleanly?

    #1668091
    Nick

    thanks – have deactivated.

    #1668528
    Elvin
    Staff
    Customer Support

    Hi there,

    If I may suggest:

    You can try this script:

    <script>
    document.addEventListener( 'scroll', function( e ) {
        const openedMenus = document.querySelector( '#mobile-header' );
    	const btn = openedMenus.querySelector( 'button.menu-toggle' );
        if(openedMenus.classList.contains('toggled')){
            btn.click();
        } 
    } );
    </script>
    #1668743
    Nick

    Thanks Elvin – that appears to work, appreciated.

    #1669928
    Elvin
    Staff
    Customer Support

    No problem. ๐Ÿ˜€

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