jqueryjquery-uijquery-ui-menu

What files do I need for this jQuery UI Menus example?


I'm trying to get this example working locally (so I can modify for my own use), but given the poor result I'm guessing that something significant is missing:

https://codepen.io/seungjaeryanlee/pen/YYoGbp

I assume it needs jQuery and jQuery UI files, and I've included those but it's not working at all. I just get a badly formed page with no sign of menu functionality.

The example works ok at the link above, but isn't clear what else I need.

Do I need to include something else?

My version follows (in 1 file for simplicity):

<html>
    <head>
        <style>
            #menubar {
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
            }

            /* Make jQuery UI Menu into a horizontal menubar with vertical dropdown */
            #menubar > li { /* Menubar buttons */
                display: inline-block;
            }
            #menubar > li > ul > li { /* Menubar buttons inside dropdown */
                display: block;
            }

            /* Change dropdown carets to correct direction */
            #menubar > li > div > span.ui-icon-caret-1-e {
                /* Caret on menubar */
                background:url(https://www.drupal.org/files/issues/ui-icons-222222-256x240.png) no-repeat -64px -16px !important;
            }
            #menubar ul li div span.ui-icon-caret-1-e {
                /* Caret on dropdowns */
                background:url(https://www.drupal.org/files/issues/ui-icons-222222-256x240.png) no-repeat -32px -16px !important;
            }
        </style>
    
        <script type="text/javascript" src = "https://code.jquery.com/jquery-1.10.2.js"></script>
        <script type="text/javascript" src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

        <script>
            $(document).ready(function() {
                $('#menubar').menu();
                
                $('#menubar').menu({
                    position: { my: 'left top', at: 'left bottom' },
                    blur: function() {
                    $(this).menu('option', 'position', { my: 'left top', at: 'left bottom' });
                    },
                    focus: function(e, ui) {
                    if ($('#menubar').get(0) !== $(ui).get(0).item.parent().get(0)) {
                        $(this).menu('option', 'position', { my: 'left top', at: 'right top' });
                    }
                    },
                });
            });
        </script>
    </head>

    <body>
        <ul id="menubar">
            <li><div>Alpha</div></li>
            <li>
                <div>Beta</div>
                <ul>
                <li><div>Beta 1</div></li>
                <li>
                    <div>Beta 2</div>
                    <ul>
                    <li><div>Beta 2a</div></li>
                    <li><div>Beta 2b</div></li>
                    </ul>
                </li>
                <li><div>Beta 3</div></li>
                </ul>
            </li>
            <li><div>Gamma</div></li>
            <li><div>Delta</div></li>
            </ul>
    </body>

</html>

Solution

  • Appears to work as expected when you have all the CSS and JS files.

    $(function() {
      $('#menubar').menu({
        position: {
          my: 'left top',
          at: 'left bottom'
        },
        blur: function() {
          $(this).menu('option', 'position', {
            my: 'left top',
            at: 'left bottom'
          });
        },
        focus: function(e, ui) {
          if ($('#menubar').get(0) !== $(ui).get(0).item.parent().get(0)) {
            $(this).menu('option', 'position', {
              my: 'left top',
              at: 'right top'
            });
          }
        },
      });
    });
    #menubar {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
    }
    
    /* Make jQuery UI Menu into a horizontal menubar with vertical dropdown */
    
    #menubar>li {
      /* Menubar buttons */
      display: inline-block;
    }
    
    #menubar>li>ul>li {
      /* Menubar buttons inside dropdown */
      display: block;
    }
    
    
    /* Change dropdown carets to correct direction */
    
    #menubar>li>div>span.ui-icon-caret-1-e {
      /* Caret on menubar */
      background: url(https://www.drupal.org/files/issues/ui-icons-222222-256x240.png) no-repeat -64px -16px !important;
    }
    
    #menubar ul li div span.ui-icon-caret-1-e {
      /* Caret on dropdowns */
      background: url(https://www.drupal.org/files/issues/ui-icons-222222-256x240.png) no-repeat -32px -16px !important;
    }
    <link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
    <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
    <ul id="menubar">
      <li>
        <div>Alpha</div>
      </li>
      <li>
        <div>Beta</div>
        <ul>
          <li>
            <div>Beta 1</div>
          </li>
          <li>
            <div>Beta 2</div>
            <ul>
              <li>
                <div>Beta 2a</div>
              </li>
              <li>
                <div>Beta 2b</div>
              </li>
            </ul>
          </li>
          <li>
            <div>Beta 3</div>
          </li>
        </ul>
      </li>
      <li>
        <div>Gamma</div>
      </li>
      <li>
        <div>Delta</div>
      </li>
    </ul>