Wednesday, October 12, 2011

Creating a menu

Well there are two ways to create menu that I can think of. One is the Google's way, you click on it to get the menu items. Other is which we see on other sites when you hover on menu and it shows you the menu items.
But we will go the Google's way.

Required: Javascript, HTML

Using:
    1. <html>
    2. <script type="text/javascript">
    3. var toggle = 1; 
    4. function showmenuitems()
    5. {
    6.    if(toggle == 1)
    7.    {
    8.       document.getElementById('menuitems').style.visibility = 'visible';
    9.       toggle = 0;
    10.    }
    11.    else
    12.    {
    13.       hidemenuitems();
    14.       toggle = 1;
    15.    }
    16. }
    17. function hidemenuitems()
    18. {
    19.       document.getElementById('menuitems').style.visibility = 'hidden';
    20. }
    21. </script>
    22. <body>
    23. <p id="menu" onclick="showmenuitems()">Menu</p>
    24. <dl onblur="hidemenuitems()" id="menuitems" style="visibility:hidden; z-index:1">
    25. <dt>Item1</dt>
    26. <dt>Item2</dt>
    27. </dl>
    28. </body>
    29. </html>
Explanation:
If you will read the code you will find that all is done is using the visibility property of the dl. Here toggle variable helps us to determine the current state. A lot can be done with this code. For ex: if you click on the document except the paragraph menu items should hide, adding href to items etc etc.

Note: It is just basic way to implement the menu. Use your imagination to create something new.

No comments:

Post a Comment

Thank you for your comment!