javascript - Vanilla JS mobile menu works on chrome but not on Firefox or Edge (no toggling) -


i use following code in site display mobile menu in viewports equal to, or under 768px.

the code works fine in chrome not in firefox , edge, where, after resizing, menu won't open (no toggling). why?

document.addeventlistener('domcontentloaded', ()=>{      let clicks = 0;     let menu = document.queryselector('#menu-mainmenu'); // menu identifier.     let menubutton = document.queryselector('.menubutton'); // button class in pagebuilder.      let mobilebehavior = ()=>{         menu.style.display = 'none';         menubutton.addeventlistener('click', ()=>{             clicks++;             if (clicks % 2) {                 menu.style.display = 'block';             } else {                 menu.style.display = 'none';             }         });     };      if (window.innerwidth <= 768) {         mobilebehavior();     }      // event cleanups:      window.addeventlistener('resize', ()=>{         if (window.innerwidth <= 768) {             clicks = 1; // prevent menubutton double clicking.             mobilebehavior(); // display mobile menu both on bootstrapping , resize.         } else if (window.innerwidth >= 769) {              menu.style.display = 'block'; // prevent display:none mobile mode.         }     });  }); 

note: no console errors.

each time resize screen add click event same menu element, should take addeventlistener outside of mobilebehavior function , exec mobilebehavior function show or hide menu, no attaching event listener, may overload listener menu.

try this:

document.addeventlistener('domcontentloaded', ()=>{      let clicks = 0;     let menu = document.queryselector('#menu-mainmenu'); // menu identifier.     let menubutton = document.queryselector('.menubutton'); // button class in pagebuilder.  menubutton.addeventlistener('click', ()=>{             clicks++;             if (clicks % 2) {                 menu.style.display = 'block';             } else {                 menu.style.display = 'none';             }         });      let mobilebehavior = ()=>{         menu.style.display = 'none';     };      if (window.innerwidth <= 768) {         mobilebehavior();     }      // event cleanups:      window.addeventlistener('resize', ()=>{         if (window.innerwidth <= 768) {             clicks = 1; // prevent menubutton double clicking.             mobilebehavior(); // display mobile menu both on bootstrapping , resize.         } else if (window.innerwidth >= 769) {              menu.style.display = 'block'; // prevent display:none mobile mode.         }     });  }); 

here have working example code demo


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -