Website Left Nav Example (with submenus built from JavaScript) 1 : Menu 2 « GUI Components « JavaScript DHTML






Website Left Nav Example (with submenus built from JavaScript) 1

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>Website Left Nav Example (with submenus built from JavaScript)</title>

        <!-- Standard reset and fonts -->
        <link rel="stylesheet" type="text/css" href="./build/reset/reset.css">
        <link rel="stylesheet" type="text/css" href="./build/fonts/fonts.css">

        <!-- Grids -->
        <link rel="stylesheet" type="text/css" href="./build/grids/grids.css">

        <!-- CSS for Menu -->
        <link rel="stylesheet" type="text/css" href="./build/menu/assets/menu.css">
 
        <!-- Page-specific styles -->
        <style type="text/css">

            div.yui-b p {
            
                margin:0 0 .5em 0;
                color:#999;
            
            }
            
            div.yui-b p strong {
            
                font-weight:bold;
                color:#000;
            
            }
            
            div.yui-b p em {

                color:#000;
            
            }            
            
            h1 {

                padding:.25em .5em;
                background-color:#ccc;

            }

            div.yuimenu {
            
                position:absolute;
                visibility:hidden;
            
            }

            #productsandservices {
            
                position:static;
                visibility:visible;
            
            }

        </style>

        <!-- Namespace source file -->
        <script type="text/javascript" src="./build/yahoo/yahoo.js"></script>

        <!-- Dependency source files -->
        <script type="text/javascript" src="./build/event/event.js"></script>
        <script type="text/javascript" src="./build/dom/dom.js"></script>

        <!-- Container source file -->
        <script type="text/javascript" src="./build/container/container_core.js"></script>

        <!-- Menu source file -->
        <script type="text/javascript" src="./build/menu/menu.js"></script>

        <!-- Page-specific script -->
        <script type="text/javascript">

            // "load" handler for the window

            YAHOO.example.onWindowLoad = function(p_oEvent) {
    

                // Hides submenus of the root Menu instance
                
                function hideSubmenus() {

                    if(oMenu.activeItem) {
                    
                        var oSubmenu = oMenu.activeItem.cfg.getProperty("submenu");

                        if(oSubmenu) {
                        
                            oSubmenu.hide();
                        
                        }
                    
                    }

                }


                // Cancels the call to "hideSubmenus"

                function cancelTimer() {

                    if(nTimeoutId) {
    
                        window.clearTimeout(nTimeoutId);
    
                    }
                
                }

    
                // "mouseout" event handler for each submenu
                
                function onSubmenuMouseOut(p_sType, p_aArguments, p_oMenu) {
    
                    cancelTimer();
    
                    nTimeoutId = window.setTimeout(hideSubmenus, 750);
                
                }


                // "beforeshow" handler for each submenu

                function onMenuBeforeShow(p_sType, p_sArgs) {

                    // Check if the menu has any items. If not, add them
                    
                    if(this.getItemGroups().length == 0) {

                        var aItemsData = this.itemsData,
                            nItems = aItemsData.length,
                            oItemData,
                            oItemConfig,
                            oSubmenu;


                        for(var i=0; i<nItems; i++) {

                            oItemData = aItemsData[i];

                            if(oItemData) {

                                oItemConfig = {};
    
                                if(oItemData.url) {
                                
                                    oItemConfig.url = oItemData.url;
                                
                                }
                                
    
                                if(oItemData.submenuItems) {
    
                                    oSubmenu = new YAHOO.widget.Menu(oItemData.submenuId);

                                    oSubmenu.itemsData = oItemData.submenuItems;

                                    oSubmenu.beforeShowEvent.subscribe(onMenuBeforeShow, oSubmenu, true);
                                    oSubmenu.mouseOverEvent.subscribe(cancelTimer);
                                    oSubmenu.mouseOutEvent.subscribe(onSubmenuMouseOut, oSubmenu, true);
    
                                    oItemConfig.submenu = oSubmenu;
                                
                                }
    
                                // Add the new YAHOO.widget.MenuItem instance to the Menu
    
                                this.addItem(new YAHOO.widget.MenuItem(oItemData.text, oItemConfig));

                            }

                        }


                        // Render the submenu into its parent MenuItem instance's element

                        this.render(this.parent.element);
                        
                    }
                
                }


                var nTimeoutId;


                /*
                     Use a JSON-formatted data structure to define the data for 
                     the submenus of the root menu
                */

                var oMenuData = {
                
                    "communications": [ 
                    
                        { text: "360", url: "http://360.yahoo.com" },
                        { text: "Alerts", url: "http://alerts.yahoo.com" },
                        { text: "Avatars", url: "http://avatars.yahoo.com" },
                        { text: "Groups", url: "http://groups.yahoo.com " },
                        { text: "Internet Access", url: "http://promo.yahoo.com/broadband" },
                        { text: "PIM", submenuId: "pim", submenuItems: [

                                { text: "Yahoo! Mail", url:"http://mail.yahoo.com" },
                                { text: "Yahoo! Address Book", url:"http://addressbook.yahoo.com" },
                                { text: "Yahoo! Calendar",  url:"http://calendar.yahoo.com" },
                                { text: "Yahoo! Notepad", url:"http://notepad.yahoo.com" }

                            ]
                        
                        }, 
                        { text: "Member Directory", url: "http://members.yahoo.com" },
                        { text: "Messenger", url: "http://messenger.yahoo.com" },
                        { text: "Mobile", url: "http://mobile.yahoo.com" },
                        { text: "Photos", url: "http://photos.yahoo.com" },
                    
                    ],

                    "shopping": [

                        { text: "Auctions", url: "http://auctions.shopping.yahoo.com" },
                        { text: "Autos", url: "http://autos.yahoo.com" },
                        { text: "Classifieds", url: "http://classifieds.yahoo.com" },
                        { text: "Flowers & Gifts", url: "http://shopping.yahoo.com/b:Flowers%20%26%20Gifts:20146735" },
                        { text: "Points", url: "http://points.yahoo.com" },
                        { text: "Real Estate", url: "http://realestate.yahoo.com" },
                        { text: "Travel", url: "http://travel.yahoo.com" },
                        { text: "Wallet", url: "http://wallet.yahoo.com" },
                        { text: "Yellow Pages", url: "http://yp.yahoo.com" }
                    
                    ],

                    "entertainment": [
                    
                        { text: "Fantasy Sports", url: "http://fantasysports.yahoo.com" },
                        { text: "Games", url: "http://games.yahoo.com" },
                        { text: "Kids", url: "http://www.yahooligans.com" },
                        { text: "Music", url: "http://music.yahoo.com" },
                        { text: "Movies", url: "http://movies.yahoo.com" },
                        { text: "Radio", url: "http://music.yahoo.com/launchcast" },
                        { text: "Travel", url: "http://travel.yahoo.com" },
                        { text: "TV", url: "http://tv.yahoo.com" }              
                    
                    ],

                    "information": [
                    
                        { text: "Downloads", url: "http://downloads.yahoo.com" },
                        { text: "Finance", url: "http://finance.yahoo.com" },
                        { text: "Health", url: "http://health.yahoo.com" },
                        { text: "Local", url: "http://local.yahoo.com" },
                        { text: "Maps & Directions", url: "http://maps.yahoo.com" },
                        { text: "My Yahoo!", url: "http://my.yahoo.com" },
                        { text: "News", url: "http://news.yahoo.com" },
                        { text: "Search", url: "http://search.yahoo.com" },
                        { text: "Small Business", url: "http://smallbusiness.yahoo.com" },
                        { text: "Weather", url: "http://weather.yahoo.com" }
                    
                    ]

                };


                // Initialize the root menu

                var oMenu = new YAHOO.widget.Menu("productsandservices", { position: "static" });


                // Initialize the submenus of the items in the root menu

                var oCommunication = new YAHOO.widget.Menu("communications"),
                    oShopping = new YAHOO.widget.Menu("shopping"),
                    oEntertainment = new YAHOO.widget.Menu("entertainment"),
                    oInformation = new YAHOO.widget.Menu("information");


                /*
                     Add a property ("itemsData") to each submenu that 
                     is reference to the data for its items.
                     This data will be used in the "beforeshow" handler to add
                     the items to each submenu before it is displayed.
                */

                oCommunication.itemsData = oMenuData["communications"];
                oShopping.itemsData = oMenuData["shopping"];
                oEntertainment.itemsData = oMenuData["entertainment"];
                oInformation.itemsData = oMenuData["information"];


                // Assign event handlers

                // Add a "mouseover" event handler to the root menu

                oMenu.mouseOverEvent.subscribe(cancelTimer);


                // Add a "beforeshow" event handler to each submenu
                
                oCommunication.beforeShowEvent.subscribe(onMenuBeforeShow, oCommunication, true);
                oShopping.beforeShowEvent.subscribe(onMenuBeforeShow, oShopping, true);
                oEntertainment.beforeShowEvent.subscribe(onMenuBeforeShow, oEntertainment, true);
                oInformation.beforeShowEvent.subscribe(onMenuBeforeShow, oInformation, true);


                // Add a "mouseover" event handler to each submenu
                
                oCommunication.mouseOverEvent.subscribe(cancelTimer);
                oShopping.mouseOverEvent.subscribe(cancelTimer);
                oEntertainment.mouseOverEvent.subscribe(cancelTimer);
                oInformation.mouseOverEvent.subscribe(cancelTimer);
                

                // Add a "mouseout" event handler to each submenu

                oCommunication.mouseOutEvent.subscribe(onSubmenuMouseOut, oCommunication, true);
                oShopping.mouseOutEvent.subscribe(onSubmenuMouseOut, oShopping, true);
                oEntertainment.mouseOutEvent.subscribe(onSubmenuMouseOut, oEntertainment, true);
                oInformation.mouseOutEvent.subscribe(onSubmenuMouseOut, oInformation, true);


                // Add a "click" handler to the document

                YAHOO.util.Event.addListener(document, "click", hideSubmenus);


                // Add the submenus to the items in the root menu 

                oMenu.getItem(0).cfg.setProperty("submenu", oCommunication);
                oMenu.getItem(1).cfg.setProperty("submenu", oShopping);
                oMenu.getItem(2).cfg.setProperty("submenu", oEntertainment);
                oMenu.getItem(3).cfg.setProperty("submenu", oInformation);


                // Render the root menu

                oMenu.render();

            }


            // Add a "load" handler for the window

            YAHOO.util.Event.addListener(window, "load", YAHOO.example.onWindowLoad);

        </script>   

    </head>
    <body id="yahoo-com">

        <div id="doc" class="yui-t1">
            <div id="hd">
                <!-- start: your content here -->

                    <h1>Website Left Nav Example (with submenus built from JavaScript) <em>[<a href="index.html">Examples Home</a>]</em></h1>
        
                <!-- end: your content here -->
            </div>
            <div id="bd">

                <!-- start: primary column from outer template -->
                <div id="yui-main">
                    <div class="yui-b">
                        <!-- start: stack grids here -->

                        <p><strong>NOTE:</strong> <em>This example demonstrates how to combine a menu built from existing markup with a menu built completely from JavaScript.  The root menu in the left nav is constructed using markup and enables the user to navigate to different landing pages for each product category.  If JavaScript is enabled, submenus are constructed and appended to the root menu.  This allows the user to skip the product landing pages and proceed directly to a given property.  Alternatively, you can <a href="leftnavfrommarkup.html">add submenus to a menu using markup</a></em></p>
                        <p>eugiat dolor, id aliquam leo tortor eget odio. Pellentesque orci arcu, eleifend at, iaculis sit amet, posuere eu, lorem. Aliquam erat volutpat. Phasellus vulputate. Vivamus id erat. Nulla facilisi. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Nunc gravida. Ut euismod, tortor eget convallis ullamcorper, arcu odio egestas pede, ut ornare urna elit vitae mauris. Aenean ullamcorper eros a lacus. Curabitur egestas tempus lectus. Donec et lectus et purus dapibus feugiat. Sed sit amet diam. Etiam ipsum leo, facilisis ac, rutrum nec, dignissim quis, tellus. Sed eleifend.</p>
                        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sit amet metus. Nunc quam elit, posuere nec, auctor in, rhoncus quis, dui. Aliquam erat volutpat. Ut dignissim, massa sit amet dignissim cursus, quam lacus feugiat dolor, id aliquam leo tortor eget odio. Pellentesque orci arcu, eleifend at, iaculis sit amet, posuere eu, lorem. Aliquam erat volutpat. Phasellus vulputate. Vivamus id erat. Nulla facilisi. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Nunc gravida. Ut euismod, tortor eget convallis ullamcorper, arcu odio egestas pede, ut ornare urna elit vitae mauris. Aenean ullamcorper eros a lacus. Curabitur egestas tempus lectus. Donec et lectus et purus dapibus feugiat. Sed sit amet diam. Etiam ipsum leo, facilisis ac, rutrum nec, dignissim quis, tellus. Sed eleifend.</p>

                        <!-- end: stack grids here -->
                    </div>
                </div>
                <!-- end: primary column from outer template -->

                <!-- start: secondary column from outer template -->
                <div class="yui-b">

                    <div id="productsandservices" class="yuimenu">
                        <div class="bd">
                            <ul class="first-of-type">
                                <li class="yuimenuitem"><a href="http://communication.yahoo.com">Communication</a></li>
                                <li class="yuimenuitem"><a href="http://shopping.yahoo.com">Shopping</a></li>
                                <li class="yuimenuitem"><a href="http://entertainment.yahoo.com">Entertainment</a></li>
                                <li class="yuimenuitem">Information</li>
                            </ul>
                        </div>
                    </div>
                    
                </div>
                <!-- end: secondary column from outer template -->
            </div>
            <div id="ft">

                <p>FOOTER: .</p>

            </div>
        </div>
        
    </body>
</html>

           
         
  








yui.zip( 3,714 k)

Related examples in the same category

1.Popup Menu
2.AtJsMenu Demo
3.Example 1: Basic Menu From Existing Markup
4.Example 2: Basic Menu From Pure JavaScript
5.Example 3: Grouped MenuItem Instances From Existing Markup
6.Example 4: Grouped MenuItem Instances Using Pure JavaScript
7.Example 5: Grouped MenuItem Instances With Titles From Existing Markup
8.Example 6: Grouped MenuItem Instances With Titles Using Pure JavaScript
9.Example 7: Multi-tiered Menu From Existing Markup
10.Example 8: Multi-tiered Menu From Pure JavaScript
11.Example 9: Handling Click Events
12.Example 10: Listening For DOM-Related Events
13.Example 11: MenuItem Configuration Properties
14.Example 12: Setting Configuration Properties At Runtime
15.Example 13: Multi-tiered Menu with Progressive Rendering of Submenus
16.Website Left Nav Example (with submenus built from markup) 2
17.OS-Style Programs Menu Example
18.Website Top Nav (with submenus built from JavaScript) 3
19.Website Top Nav (with submenus built from markup) 4
20.Drop down menu and sub menu
21.Vertical menu and image menu
22.Emulate XP start menu
23.xmenu-xlayer-3
24.tree-menu
25.menu for applications