Forum Discussion

MikeImage's avatar
MikeImage
Community Member
9 months ago

Closing the side menu if open using a JavaScript trigger

Is there a way to verify if the menu is open, close it using JavaScript and if it's closed, do nothing?

At the moment I can only toggle it using this JavaScript.

document.querySelector("#hamburger > div").click();

  • you can check if menu is closed with

    document.body.classList.contains("sidebar-closed")
  • MikeImage's avatar
    MikeImage
    Community Member

    This code is not working for some reason.

    var modernPlayerMenu = document.querySelector("#hamburger > div");
    var isMenuOpen = document.body.classList.contains("sidebar-open");

    if (isMenuOpen) {
    // The menu is open, so close it
    if (modernPlayerMenu) {
    modernPlayerMenu.click();
    }
    } else {
    // The menu is already closed, do nothing
    }

  • Just out of personal curiosity, if the menu is closed, does it hurt anything to close it? If not, why check? Just close it, and it's closed whether it was open or not.

    • MikeImage's avatar
      MikeImage
      Community Member

      Trying to close the menu automatically on timeline start if it's open, then hide the hamburger menu so it not possible to open again until I decide to show it back again using JavaScript.

  • So maybe I wasn't clear. I don't ask why you want to close it; I'm asking why you need to check if it's open.  Close it when you start the timeline. If it's already closed, it doesn't change if you close it. Seems to me to be less work, and fewer moving parts. On the other hand, look at what you learned by trying it that way. Maybe it was worth it.  :)

    • MikeImage's avatar
      MikeImage
      Community Member

      Do you know if it's possible to Disable the hamburger menu after hiding it using JavaScript? The "TAB" key can still open the menu even if it's not accessible via the mouse.

      • Jürgen_Schoene_'s avatar
        Jürgen_Schoene_
        Community Member

        you can remove the hamburger button from the tab-key sequence with

        document.getElementById("hamburger").tabIndex = -1;

        and reactivate the old state with

        document.getElementById("hamburger").tabIndex = 0;

         

        if you want, you can also deactivate the hamburger menu (without hiding)

        document.getElementById("hamburger").style.pointerEvents = "none";

        and reactivate with

        document.getElementById("hamburger").style.pointerEvents = "";

         

         

  • MikeImage's avatar
    MikeImage
    Community Member

    Your right, I don't need to check if it's open. I still had the toggle trigger still in mind so I had to check if it was open to trigger the toggle code.


     var modernPlayerMenu = document.querySelector("#hamburger > div");
    var isMenuOpen = ! document.body.classList.contains("sidebar-closed");
    if (isMenuOpen) {
      modernPlayerMenu.click();
    }


  • Ah, that makes sense. Thanks.

    No, I can’t help with the hamburger menu. Sorry.

  • MikeImage's avatar
    MikeImage
    Community Member

     what would be he variable to change if I were to use the Top bar menu?