Earlier we looked at different plugins provided by jQueryUI. In this post, we will see the jQueryUI Menu plugin in action. jQueryUI provides menu()
method to create themeable menu with keyboard and mouse interaction. We can create menu from any valid markup and the most commonly used element is the unordered list. We will look at different options, methods and events associated with the jQueryUI Menu plugin in this tutorial.
jQueryUI menu plugin uses following CSS classes to style the look and feel of the menu.
ui-menu:
This is menu’s outer container. It has additionalui-menu-icons
class if it contains any icons.ui-menu-item
: this is container for individual items in the menu.ui-menu-icon
: Used to set icons for sub menu.ui-menu-divider
: This is used to divide elements between the items.
Contents
Menu Plugin Options
In this section, we will discuss about different options available to customize the jQueryUI Menu plugin. We have used many of these options in the Menu Plugin in Action section.
The above table briefly describes all the available options in the jQueryUI Menu plugin.
Menu Plugin Methods
In this section, we will look into the jQueryUI Menu plugin methods and its syntax. These methods are very useful when you deal with the Menu plugin.
The above table briefly describes all the available methods in the jQueryUI Menu plugin.
Menu Plugin Events
In this section, we will discuss about different events associated with jQueryUI Menu plugin. We have used many of these events in the Menu Plugin in Action section.
The above table briefly describes all the available events in the jQueryUI Menu plugin.
Menu Plugin in Action
In this section, we will try different examples to explore the uses of jQueryUI Menu plugin.
Default Functionality
The following example demonstrates the default functionality of jQueryUI Menu plugin, disabled items and nested menus with mouse and keyboard navigation support.
menu-default.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<!doctype html> <html lang="en"> <head> <title>jQuery UI Menu - Default Functionality</title> <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.2/themes/south-street/jquery-ui.css"> <script src="https://code.jquery.com/jquery-2.1.1.js"></script> <script src="https://code.jquery.com/ui/1.11.2/jquery-ui.js"></script> <script> $(function() { $( "#menu" ).menu(); }); </script> <style> .ui-menu { width: 150px; } </style> </head> <body> <ul id="menu"> <li>Hockey</li> <li>Football <ul> <li>Iain Hume</li> <li>Del Piero</li> <li>David James</li> </ul> </li> <li>Cricket <ul> <li>Sachin</li> <li>Ganguly</li> <li>Dravid</li> </ul> </li> <li class="ui-state-disabled">KickBoxing</li> <li>Tennis <ul> <li>Patrick Rafter</li> </ul> </li> </ul> </body> </html> |
You will see the following output in your browser.
In this example, ui-state-disabled
is used to disable any item in the menu.
Menu with Category
The following example demonstrates the menu with different categories. We use items
option to implement this functionality.
menu-category.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<!doctype html> <html lang="en"> <head> <title>jQuery UI Menu - Category</title> <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.2/themes/south-street/jquery-ui.css"> <script src="https://code.jquery.com/jquery-2.1.1.js"></script> <script src="https://code.jquery.com/ui/1.11.2/jquery-ui.js"></script> <script> $(function() { $( "#menu" ).menu({ items: "> :not(.ui-widget-header)" }); }); </script> <style> .ui-menu { width: 150px; } .ui-widget-header { padding: 0.3em; } </style> </head> <body> <ul id="menu"> <li class="ui-widget-header">Sports</li> <li>Football</li> <li>Cricket</li> <li class="ui-widget-header">Players<li> <li>Iain Hume</li> <li>David James</li> <li>Sachin</li> <li>Ganguly</li> </ul> </body> </html> |
You will see the following output in your browser.
You can see how we used items
options to create two categories in the above menu, Sports and Players.
Menu with Icons
The following example demonstrates the menu plugin with different icons used. jQuery UI provides a list of ui-icons to customize the menu plugin.
menu-icons.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<!doctype html> <html lang="en"> <head> <title>jQuery UI Menu - Icons</title> <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.2/themes/south-street/jquery-ui.css"> <script src="https://code.jquery.com/jquery-2.1.1.js"></script> <script src="https://code.jquery.com/ui/1.11.2/jquery-ui.js"></script> <script> $(function() { $( "#menu" ).menu(); }); </script> <style> .ui-menu { width: 150px; } </style> </head> <body> <ul id="menu"> <li><span class="ui-icon ui-icon-flag"></span>Flag</li> <li><span class="ui-icon ui-icon-scissors"></span>Cut <ul> <li><span class="ui-icon ui-icon-image"></span>Cut</li> <li><span class="ui-icon ui-icon-signal-diag"></span>Star</li> </ul> </li> <li><span class="ui-icon ui-icon-star"></span>Star</li> <li class="ui-state-disabled"><span class="ui-icon ui-icon-print"></span>Print</li> </ul> </body> </html> |
You will see the following output in your browser.
You can see the how we used ui-icon
CSS class to style menu’s look and feel.
That’s all for jQueryUI menu plugin and you will see more plugins in the coming posts.