问题描述
我正在使用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.