How can I make my Webflow menu automatically close after clicking a menu item that links to another section on the page?

TL;DR
  • Modify menu settings or add JavaScript to close the Webflow menu after clicking a link by simulating a toggle button click if the menu is open. 
  • Test the setup in preview mode and on the published site to ensure it works across different browsers.

To ensure your Webflow menu automatically closes after clicking a menu item linking to a section on the same page, you'll need to modify the menu settings and possibly add a bit of custom code.

1. Enable Menu Close Interaction

  • Go to the Navigator in the Webflow Designer.
  • Select the Menu element that needs to be closed after clicking.
  • Check for Interactions: If your theme or menu component comes with built-in interactions, ensure they include closing the menu when a link is clicked.

2. Add Custom Code

If the default interactions cannot handle this, you may need to add custom JavaScript code.

  • Access the Page Settings:
  • Click on the page's name in the Pages panel.
  • Open the settings for the desired page.

  

  • Insert Custom Code:
  • Locate the Before </body> tag section.
  • Add the following JavaScript code snippet:

    ```javascript

    <script>

    const navLinks = document.querySelectorAll('.menu-item-class a');

    const toggleButton = document.querySelector('.menu-button-class');

    navLinks.forEach(link => {

      link.addEventListener('click', function() {

        if (toggleButton.getAttribute('aria-expanded') == 'true') {

          toggleButton.click();

        }

      });

    });

    </script>

    ```

  • Replace.menu-item-class and .menu-button-class with your actual class names for the menu items and the button that toggles the menu.

3. Test the Functionality

  • Preview the Site: Use the Webflow Designer's preview function.
  • Click on the Menu Item: Ensure the menu closes after a section link is clicked.
  • Check in Published Site: Publish your site and verify the functionality in different browsers.

Summary

To make a Webflow menu automatically close after clicking a menu item linking to a section, configure any built-in interactions for closing the menu, or add custom JavaScript to handle this action by simulating a menu button click upon clicking a menu link. This will ensure a seamless navigation experience for users.

Rate this answer

Other Webflow Questions