Mmenu-如何检测关闭的回叫事件?

问题描述

我正在使用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/

解决方法

我在以下问题的评论中找到了我的问题的答案: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.

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...