Open In App

Foundation CSS Dropdown Menu JavaScript Reference

Foundation CSS is an open-source and responsive front-end framework built by the ZURB foundation in September 2011, that makes it easy to layout stunning responsive websites, apps, and emails that appear amazing and can be accessible to any device.

The Dropdown Menu is used for displaying an expandable dropdown menu by using the Foundation CSS plugin. There are various JavaScript Reference facilitated by the Foundation CSS that enhances the user experience along with increasing the interactivity of the site. Below is the list of JavaScript Reference that is utilized by the Dropdown Menu, is described below:



var elem = new Foundation.DropdownMenu(element, options);

Where,

The DropdownMenu#event:init fires the events, once the instances for the DropdownMenu get created.



We can pass the above-mentioned plugin options as an object to the DropdownMenu constructor in order to customize the behavior of the dropdown menu.

To listen for these events, we can use the on() method:

$('.dropdown-parent').on('show.zf.dropdown', function() {
    // Do something when dropdown menu is opened
});

To call this method, you can use the foundation() method:

$('#element').foundation('_destroy');

Now, we will look at an HTML code implementing the foundation CSS dropdown menu using JavaScript reference as an example.

Example 1: This example describes the Dropdown Menu JavaScript Reference using Foundation CSS.




<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" 
          href=
    <script src=
    </script>
    <script src=
    </script>
</head>
  
<body>
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <h3>
        Foundation CSS Dropdown Menu JavaScript Reference
    </h3>
    <ul class="vertical dropdown menu" 
        data-dropdown-menu data-responsive-menu="accordion medium-dropdown"
        style="max-width: 250px;">
        <li>
            <a href="#">Opt 1</a>
            <ul class="vertical menu nested">
                <li>
                      <a href="#">Opt 1A</a>
                  </li>
                <li>
                      <a href="#">Opt 1B</a>
                  </li>
                <li>
                      <a href="#">Opt 1C</a>
                  </li>
            </ul>
        </li>
        <li><a href="#">Opt 2</a></li>
        <li>
            <a href="#">Opt 3</a>
            <ul class="vertical menu nested">
                <li>
                      <a href="#">Opt 3A</a>
                  </li>
                <li>
                      <a href="#">Opt 3B</a>
                  </li>
                <li>
                      <a href="#">Opt 3C</a>
                  </li>
            </ul>
        </li>
        <li><a href="#">Opt 4</a></li>
    </ul>
      
    <script>
        var dropdownMenu = 
            new Foundation.DropdownMenu($('.dropdown.menu'), {
            alignment: 'auto',
            closeOnClick: true,
            disableHover: false,
            clickOpen: true,
        });
    </script>
</body>
  
</html>

Output:

 

Here, note that we have created a dropdownMenu variable to hold the instance of the dropdown menu plugin, and passed in the plugin options when creating the instance. Inside the ul element, we create 4 <li> elements. The first and third <li> elements have nested submenus, which are created by adding an additional <ul> element with the classes vertical, menu, and nested inside the corresponding li elements. Each submenu contains three nested li elements with links labeled “Opt 1A”, “Opt 1B”, and “Opt 1C”, or “Opt 3A”, “Opt 3B”, and “Opt 3C”, respectively. Finally, we load jQuery and the Foundation JavaScript files and initialize the Foundation framework using the foundation() method on the document object.

Reference: https://get.foundation/sites/docs/dropdown-menu.html#javascript-reference


Article Tags :