Search code examples
javascripthtmlmobiledrop-down-menuhamburger-menu

Get a drop-down menu to work inside/outside of a hamburger menu


I want to get the Language menu to work as a drop-down menu when it's on desktop and remain an inactive drop-down menu when it's on mobile.

Once clicked on mobile it should show the options inside the drop-down menu, remaining inside the hamburger menu.

Language menu:

https://i.sstatic.net/bik75.png

&&

https://i.sstatic.net/waYAk.png

What I want it to do on desktop:

https://i.sstatic.net/OD5zn.png

I have made the menu clickable on desktop with javascript code, but on mobile the menu acts weirdly.

I would like to see the drop-down menu work when clicked on desktop, and when I click anywhere else on the desktop it would close, not just when I click the menu itself.

On mobile the menu appears weirdly instead of being deactivated until clicked.

JSFiddle -> <script async src="//jsfiddle.net/jkuwg3ac/embed/"></script>


Solution

  • There is a better way to create a language menu using Bulma without JavaScript.

    Language menu HTML:

    <div class="select">
      <select>
        <option selected>English</option>
        <option>Italian</option>
        <option>Arabic</option>
      </select>
    </div>
    

    Here is your project using my idea:

    document.addEventListener('DOMContentLoaded', () => {
    
      // Get all "navbar-burger" elements
      const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);
    
      // Check if there are any navbar burgers
      if ($navbarBurgers.length > 0) {
    
        // Add a click event on each of them
        $navbarBurgers.forEach(el => {
          el.addEventListener('click', () => {
    
            // Get the target from the "data-target" attribute
            const target = el.dataset.target;
            const $target = document.getElementById(target);
    
            // Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
            el.classList.toggle('is-active');
            $target.classList.toggle('is-active');
    
          });
        });
      }
    
    });
      
    <!DOCTYPE html>
    <html>
    
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Test!</title>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.4/css/bulma.min.css">
      <script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
    </head>
    
    <body>
    
    
      <nav class="navbar has-text-centered" role="navigation" aria-label="main navigation">
        <div class="navbar-brand">
          <a class="navbar-item" href="https://bulma.io">
            <img src="https://bulma.io/images/bulma-logo.png" width="112" height="28">
          </a>
    
          <a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="navMenu">
            <span aria-hidden="true"></span>
            <span aria-hidden="true"></span>
            <span aria-hidden="true"></span>
          </a>
        </div>
    
        <div id="navMenu" class="navbar-menu">
          <div class="navbar-start">
            <a class="navbar-item">
                Home
              </a>
    
            <a class="navbar-item">
                Documentation
              </a>
    
          </div>
    
          <div class="navbar-end">
            <div class="select">
              <select>
                <option selected>English</option>
                <option>Italian</option>
                <option>Arabic</option>
              </select>
            </div>
    
          </div>
    
    
        </div>
      </nav>
    
    </body>
    
    </html>