Website Top Nav (with submenus built from JavaScript) 3 : Menu 2 : GUI Components JAVASCRIPT DHTML TUTORIALS


JAVASCRIPT DHTML TUTORIALS » GUI Components » Menu 2 »

 

Website Top Nav (with submenus built from JavaScript) 3




<!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 Top Nav (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;

            }

            #productsandservices {
            
                visibility: visible;
                margin:0 0 10px 0;
            
            }

        </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 Menubar instance
                
                function hideSubmenus() {

                    if(oMenuBar.activeItem) {
                    
                        var oSubmenu = oMenuBar.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 of the menubar
                
                function onSubmenuMouseOut(p_sType, p_aArguments, p_oMenu) {
    
                    cancelTimer();
    
                    nTimeoutId = window.setTimeout(hideSubmenus, 750);
                
                }
    
    
                // "mouseover" handler for each item in the menubar
    
                function onMenuBarItemMouseOver(p_sType, p_aArguments, p_oMenuItem) {
                
                    var oActiveItem = this.parent.activeItem;
                
                
                    // Hide any other submenus that might be visible
                
                    if(oActiveItem && oActiveItem != this) {
                
                        this.parent.clearActiveItem();
                
                    }
                
                
                    // Select and focus the current MenuItem instance
                
                    this.cfg.setProperty("selected"true);
                    this.focus();
                
                
                    // Show the submenu for this instance
                
                    var oSubmenu = this.cfg.getProperty("submenu");
                
                    if(oSubmenu) {
                
                        oSubmenu.show();
                
                    }
                
                }
            
    
                // "mouseout" handler for each item in the menubar
            
                function onMenuBarItemMouseOut(p_sType, p_aArguments, p_oMenuItem) {
                
                    this.cfg.setProperty("selected"false);
                
                
                    var oSubmenu = this.cfg.getProperty("submenu");
                
                    if(oSubmenu) {
                
                        var oDOMEvent = p_aArguments[0],
                            oRelatedTarget = YAHOO.util.Event.getRelatedTarget(oDOMEvent);
                
                        if(
                            !(
                                oRelatedTarget == oSubmenu.element || 
                                this._oDom.isAncestor(oSubmenu.element, oRelatedTarget)
                            )
                        ) {
                
                            oSubmenu.hide();
                
                        }
                
                    }
                
                }


                // "beforeshow" handler for each submenu of the menubar

                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 menubar
                */

                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 menubar

                var oMenuBar = new YAHOO.widget.MenuBar("productsandservices");


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

                var oCommunications = 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.
                */

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


                // Assign event handlers

                // Add a "mouseover" handler to the menubar

                oMenuBar.mouseOverEvent.subscribe(cancelTimer);


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

                oCommunications.beforeShowEvent.subscribe(onMenuBeforeShow, oCommunications, 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

                oCommunications.mouseOverEvent.subscribe(cancelTimer);
                oShopping.mouseOverEvent.subscribe(cancelTimer);
                oEntertainment.mouseOverEvent.subscribe(cancelTimer);
                oInformation.mouseOverEvent.subscribe(cancelTimer);


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

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


                // Add the submenus to the items in the menubar 

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


                // Add a "click" handler to the document

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


                /*
                    Add a "mouseover" and "mouseout" event handler each item 
                    in the menubar 
                */               

                var aMenuBarItems = oMenuBar.getItemGroups()[0],
                    oMenuItem,
                    i = aMenuBarItems.length - 1;

                do {

                    oMenuItem = aMenuBarItems[i];

                    oMenuItem.mouseOverEvent.subscribe(onMenuBarItemMouseOver, oMenuItem, true);
                    oMenuItem.mouseOutEvent.subscribe(onMenuBarItemMouseOut, oMenuItem, true);
                
                }
                while(i--);


                // Render the menubar

                oMenuBar.render();
                
            }


            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 Top Nav (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 -->

                        <div id="productsandservices" class="yuimenubar">
                            <div class="bd">
                                <ul class="first-of-type">
                                    <li class="yuimenubaritem"><a href="http://communication.yahoo.com">Communication</a></li>
                                    <li class="yuimenubaritem"><a href="http://shopping.yahoo.com">Shopping</a></li>
                                    <li class="yuimenubaritem"><a href="http://entertainment.yahoo.com">Entertainment</a></li>
                                    <li class="yuimenubaritem">Information</li>
                                </ul>
                            </div>
                        </div>

                        <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 top 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="topnavfrommarkup.html">add submenus to a menubar using markup</a></em></p>

                        <form name="example">
                            <select name="test">
                                <option value="one">One</option>
                                <option value="two">Two</option>
                                <option value="three">Three</option>                                
                            </select>
                        </form>

                        <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>
                        <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">

                    <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>
                    
                </div>
                <!-- end: secondary column from outer template -->
            </div>
            <div id="ft">

                <p>FOOTER: 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.</p>

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

Download : Download nav_yui.zip nav_yui.zip


-

Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .

Follow Navioo On Twitter

JAVASCRIPT DHTML TUTORIALS

 Navioo GUI Components
» Menu 2