【问题标题】:Mmenu - How do I detect the closed call back event?Mmenu - 如何检测关闭的回调事件?
【发布时间】:2020-11-12 10:16:18
【问题描述】:

我正在使用Jquery Mmenu plugin,我想检测何时发生关闭的回调事件,以便我可以从我的汉堡菜单中删除一个 CSS 类。我有打开和关闭菜单的相同汉堡菜单图标,我在菜单打开时添加了一个 is-active 类,但我需要检测整个菜单何时关闭,以便我可以删除该类,但我似乎无法想办法。

这是我目前所拥有的:

document.addEventListener(
    "DOMContentLoaded", () => {
        const menu = new Mmenu( "#mobile-menu", {
            "extensions": [
                "position-front"
            ],
            scrollBugFix: {
                fix: true
            },
    });
    const api = menu.API;
    const hamburger = document.querySelector('.hamburger');

    hamburger.addEventListener(
       "click", ( evnt ) => {
           evnt.preventDefault();
           hamburger.classList.add("is-active");
     });
    }
);


  [1]: https://mmenujs.com/

【问题讨论】:

    标签: javascript jquery css class mmenu


    【解决方案1】:

    我在这个问题的 cmets 中找到了我的问题的答案:Callback event when jQuery 'mmenu' opened

    添加:

    // When the open callback event is fired bind to it and add the class
    api.bind('open:after', function (){
        hamburger.classList.add('is-active');
        console.log('open')
    })
    
    // When the closed callback event is fired bind to it and remove the class
    api.bind('close:after', function(){
        hamburger.classList.remove("is-active");
        console.log('closed')
    })
    
    The documentation for the plugin is good but not that great so I struggled for a while before seeing this. Maybe it can help someone else.
    
    I am using the latest version of the plugin and that is 8 something.
    

    【讨论】:

    • 感谢@Neelam,这对我来说非常有用并且有效!请分享有关 mmenu.js 的更多知识。
    猜你喜欢
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 2013-05-11
    相关资源
    最近更新 更多