YUI Library Home

YUI Library Examples: Menu Family: Website Top Nav Using Animation With Submenus From JavaScript

Menu Family: Website Top Nav Using Animation With Submenus From JavaScript

This example demonstrates how to create a traditional, two-column page layout (using Grids) with top navigation featuring animated drop-down menus. The top navigation is created using a MenuBar instance that is constructed using existing markup and enables the user to navigate to different landing pages for each Yahoo! product category. If JavaScript is enabled, submenus are appended to the items in the MenuBar, allowing the user to skip the product landing pages and proceed directly to a given property.

Begin by placing the markup for the two-column Grid on the page (this example uses the Grids Preset Template 1, 160px left). Add the markup for the root MenuBar instance to the right column of the grid, appending the class of "yuimenubarnav" to the root element. The application of the "yuimenubarnav" class will render each item in the MenuBar instance with arrows to the right of each text label, providing a visual cue that the item contains a submenu.

Use the onContentReady method of the Event utility to instantiate the MenuBar as soon as its markup is available for scripting.

This MenuBar instance makes use of several configuration properties. Setting the "autosubmenudisplay" configuration property to "true" modifies its default behavior so that mousing over any item in the MenuBar automatically triggers the display of its submenu. The "hidedelay" configuration property is set to "750" so each submenu automatically hides 750ms after the user's mouse has left the menu. Lastly, the "lazyload" property is set to "true" to help speed up the initial load time of the MenuBar instance by deferring the initialization and rendering of each submenu until just before it is initially made visible.

Submenus are added to each item in the MenuBar by subscribing to the "beforeRender" event and setting the "submenu" configuration property of each MenuBarItem instance to an object literal containing the necessary data to create the submenu.

Setup the animation for the submenus by subscribing to their "beforeShow" and "show" events. The "beforeShow" event handler will be used to collapse the height of the Menu instance before it is made visible. The "show" event handler will kick off an animation that increments the "marginTop" style property of each submenu's <ul> element from a negative value equal to its height to 0, causing the submenu to slowly expand to its original height as it is displayed.

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

Menu Family Examples:

More Menu Family Resources:

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings