How to implement automatic closing of the navigation menu after clicking an anchor link in Elementor

图片[1]-如何在Elementor中实现点击锚链接后自动关闭导航菜单-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Setting up a directory to close after clicking an anchor link in Elementor (such as a collapsed menu or navigation menu) can be done using some custom code. Here are the specific steps:

Step 1: Edit Individual Post Templates

  1. log inWordPress DashboardThe
  2. Navigate to Elementortemplates > Saved templatesThe
图片[2]-如何在Elementor中实现点击锚链接后自动关闭导航菜单-光子波动网 | 专业WordPress修复服务,全球范围,快速响应
  1. Find your individual post template and click "Edit with Elementor".

Step 2: Adding HTML Elements

  1. In the template editor, find the section where you need to add the code, usually the end of the page.
  2. Drag and drop an HTML element into the template.
图片[3]-如何在Elementor中实现点击锚链接后自动关闭导航菜单-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Step 3: Paste the custom JavaScript code

In the HTML element, paste the following JavaScript code:

<script
document.addEventListener('DOMContentLoaded', function() {
    var menuItems = document.querySelectorAll('.elementor-table-of-contents a'); // replace .elementor-table-of-contents with your catalog class

    menuItems.forEach(function(menuItem) {
        menuItem.addEventListener('click', function() {
            var menuToggle = document.querySelector('.elementor-menu-toggle'); // replace .elementor-menu-toggle with your menu toggle button class

            if (menuToggle) {
                menuToggle.click(); // trigger the menu to close
            }
        }).
    });
}).
</script

explanation

  1. class name matching::
    • .elementor-table-of-contents is the class name of the directory, you need to replace it with the actual directory class name.
    • .elementor-menu-toggle is the class name of the menu toggle button, you need to replace it with the actual menu toggle button class name.
    You can check the actual class name used via your browser's developer tools (F12).
  2. event listener::
    • document.addEventListener('DOMContentLoaded', function() { ... }); Make sure the code is executed after the page is fully loaded.
    • menuItem.addEventListener('click', function() { ... }); Add a click event listener for each catalog link.
  3. Trigger menu off::
    • menuToggle.click(); Simulates clicking the menu toggle button, which closes the menu.

Step 4: Save and Publish

  1. Name your custom code (e.g. "Anchor Links Close Directory").
  2. Select "Entire site" as the scope of application for the code.
  3. Save and publish the code.
图片[4]-如何在Elementor中实现点击锚链接后自动关闭导航菜单-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Summary:

By adding custom JavaScript code to a single post template, you can implement an automatic closure of the directory after Elementor clicks on an anchor link. Key steps include editing the template, adding HTML elements, and pasting the code. Make sure to match the class name correctly to ensure the feature works properly. This setup not only optimizes the use of the navigation menu, but also enhances the overall user experience and interactivity of the site. Choosing this method helps keep the site professional and functional.


Contact Us
Can't read the article? Contact us for free answers! Free help for personal, small business sites!
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
© Reprint statement
Author: xiesong
THE END
If you like it, support it.
kudos0 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments