This Season
 

Javascript Menu Tutorial

Dynamic HTML, or DHTML, is the technique of combining JavaScript actions, HTML formatting and CSS style to add interactivity to a web page. One of the most popular applications of DHTML is to create drop-down site navigation menus. Drop-down menus allow you to give your visitors a way to navigate your site easily, provide a lot of links in a compact form and lend your site a professional look and feel.

Related Searches:
    1. Build the Menu

      • The first step in building a drop-down menu is to build the menu itself. The neatest and most organized way to do this is to use a nested, unordered list. Place each of the main menu elements at the top level of the list. Place each submenu as a list nested in the corresponding main menu list item. For example, here is a menu with two menu items, each with a submenu containing three items.

        <ul>
        <li>
        <a href="item1.html">Menu 1</a>
        <ul>
        <li><a href="item11.html">Item 11</a></li>
        <li><a href="item12.html">Item 12</a></li>
        <li><a href="item13.html">Item 13</a></li>
        </ul>
        </li>
        <li>
        <a href="item2.html">
        Menu 2</a>
        <ul>
        <li><a href="item21.html">Item 21</a></li>
        <li><a href="item22.html">Item 22</a></li>
        <li><a href="item23.html">Item 23</a></li>
        </ul>
        </li>
        </ul>

      Styling the Menu

      • Now it is time to turn your bulleted list into something that looks like a menu. Create two classes for the unordered lists, "menu" and "submenu," and apply the following styles to the various list tags.

        Turn off the list bullets. The z-index is for positioning:

        ul.menu, ul.submenu {
        list-style: none;
        z-index: 5;
        }

        Hide the submenus:

        ul.submenu {
        display: none;
        }

        Make the main menu display horizontally. Float will cause the menus to drop down correctly:

        ul.menu li {
        display: inline;
        float: left;
        }

        Set the width of each menu item:

        ul.menu li, ul.submenu li {
        width: 6em ;
        }

        Attach the style classes to the menus, like in the following snippet of the menu HTML code from earlier:

        <ul class="menu" id="menu">
        <li>
        <a href="item1.html">Menu 1</a>
        <ul class="submenu" >
        ...

      Styling the Menu Position

      • Wrap the entire unordered list in two div elements, giving them style classes and ids. The ids will be used later to position the menu.

        <div class="menucontainer" id="menucontainer">
        <div class="menucontent" id="menucontent">
        ...[menu HTML code]
        </div>
        </div>
        <br clear="all">

        Add style classes to the CSS code. The height setting will force the page content to wrap correctly around the menu:

        div.menucontainer, ul.menu li, ul.submenu li {
        height: 1.1em;
        }

        Absolute positioning will cause the submenus to float over the rest of the content of the page. The position will be changed later by the JavaScript:

        div.menucontent {
        position: absolute;
        left: 0px;
        top: 0px;
        z-index: 3;
        }

      Setting the Menu Positioning

      • Create a JavaScript script area in the header of the page and add the following code. The function will figure out where the "menu container" div is located and move the menu to that position.

        var toppos = 0 ;
        var leftpos = 0 ;
        function setPosition(){
        if(document.getElementById("menucontainer") == null ||
        document.getElementById("menucontent") == null){
        return;
        }
        toppos = document.getElementById("menucontainer").offsetTop ;
        leftpos = document.getElementById("menucontainer").offsetLeft ;
        document.getElementById("menucontent").style.top = toppos + "px";
        document.getElementById("menucontent").style.left = leftpos + "px" ;
        }

        Call the setPosition function in your page's body tag:

        <body onload="setPosition();">

      Adding Menu Functionality

      • Add the following code to the JavaScript. This code contains show and hide functions that modify the style of the page element passed in as "el."

        function show(el){
        if(document.getElementById(el) == null){
        return;
        }
        document.getElementById(el).style.display = "block";
        }
        function hide(el){
        if(document.getElementById(el) == null){
        return;
        }
        document.getElementById(el).style.display = "none";
        }

        Add a unique id, and mouse event handlers that call the show and hide functions to each submenu. The id must match the parameter passed to the show and hide functions. Each submenu must have a unique id (for instance, submenu1, submenu2). The following shows the changes to a snippet of the code from earlier:

        <ul class="submenu" id="submenu1" onmouseover="show('submenu1')" onmouseout="hide('submenu1')">

        Add mouse event handlers that call the show and hide functions to each main menu item. Pass those functions the corresponding submenu id (for example, Menu 1 will call the show/hide functions for submenu1, Menu 2 for submenu2). The following shows the changes to a snippet of the code from earlier:

        <ul class="menu" id="menu">
        <li onmouseover="show('submenu1')" onmouseout="hide('submenu1')">
        <a href="item1.html">Menu 1</a>

        Your drop-down menu is complete.

      Wrap Up

      • Finish off your menus by expanding on the CSS. Add borders, background colors and padding, for example. Tweak the padding and margins to achieve a polished appearance. Remember to test your page in different browsers to make sure the menu displays and functions correctly no matter which browser your visitors use.

    Related Searches

    References

    Resources

    Read Next:

    Comments

    You May Also Like

    • HTML List Tutorial

      Lists are a wonderful way to keep things organized on a website. Creating lists using HyperText Markup Language (HTML) is easy. There...

    • Dropdown Menu JavaScript Tutorial

      A drop-down menu makes it possible to display large amounts of data in small amount of space. Web developers use these menus...

    • Tutorial on How to Pull Down Menus in Java Script

      JavaScript allows you to create dynamic drop (pull)-down menus. JavaScript code is normally enclosed directly in the HTML document, which removes the...

    • Tutorial on Pull-Down Menus in JavaScript

      JavaScript pull-down menus are a way to create graphically enhanced menus for user navigation in web pages. These menus have become a...

    • Tutorial on the Pop-up Menu in Javascript

      Depending on the total number of links contained within your website, the amount of page space a fixed menu takes can be...

    • JavaScript Drop-down Tutorial

      JavaScript is a client-side scripting language used to create dynamic web pages. A client-side scripting language is implemented in the user's web...

    • HTML Javascript Tutorial

      HyperText Markup Language (HTML) is the markup language used to build a Web page, while JavaScript is a scripting language that builds...

    • Tutorial for a Cascading Menu in Java Script

      Web page menus are designed to allow visitors to navigate the encompassing website with ease. With smaller websites, standard HTML coded menus...

    • How to Disable CSS Hover With JavaScript

      Hypertext Markup Language (HTML) gives programmers' access to Web pages through the Document Object Model or DOM. A knowledgeable programmer can inspect...

    • HTML Menu Tutorial

      A menu allows you to connect all of the pages on your website. Without one, each page would be its own entity...

    • Menu Designing in Javascript

      JavaScript is a Web language built into all major browsers that you can use to make your Web pages do a lot...

    • Javascript Events Tutorial

      Events in JavasScript are used to execute actions when certain things happen. For example, if you want to make a message pop...

    • Easy Dynamic Menus for a Web Site

      A website menu, or navigational bar, plays a vital role in how a user can explore a website. A dynamic menu, in...

    • JavaScript Tutorial for Mouseover Events

      When you move your mouse over anything on a web page, anything can happen. A page element can change colors, fade away...

    • Javascript Navigation Tutorial

      Javascript allows programmers to navigate to other web pages using client-side code. This is beneficial when you want to dynamically send users...

    • Javascript Combobox Tutorial

      Combo boxes are boxes used in HTML forms that contain text options that can be selected. Combo boxes can take the form...

    • Hands-On JavaScript Tutorial

      Create a simple HTML document and save it as backgroundcolor.html. <HTML> <HEAD> </HEAD> <BODY> </BODY> </HTML>

    • An HTML Drop Down Menu Tutorial

      Web forms are used on HTML pages to collect data from visitors. Forms allow for several types of input fields including text...

    • Dreamweaver Tutorial in JavaScript

      Adobe Dreamweaver has a feature called a "behavior" that writes JavaScript code in your Web pages for you. Adobe defines a behavior...

    • How to Add Items to a Combo Box With JavaScript

      Right-click the HTML file on your desktop and select "Open With." Choose "Notepad" from the list of programs and click "OK." This...

    Follow eHow

    Related Ads