|
|
|
|
|
Create a Javascript MenuHow to Build a Simple Yet Effective Menu with Javascript
This tutorial shows just how easy it is to develop a menu for a web page - all though the power of Javascript.
Javascript provides the web page developer with an excellent and powerful programming tool - which is, of course, why it forms the core of that current hot Internet technique: Ajax (Asynchronous Javascript and XML). However, Javascript can be of benefit to any web site - especially when it comes to manipulating objects on a web page: objects such a menus. The Basics of a Javascript MenuIn order to create a menu the web site developer needs to make use of two Javascript methods:
The web site developer also needs to make use of the web page's object's style, and in particular the object's:
By combining these Javascript methods and by using Javascript code to manipulate each object's style the developer can hide and reveal objects at will i.e. create a menu. Using Javascript to Hide and to Show Objects on a Web PageThe concept of the menu is quite simple:
and this can be demonstrated by some simple Javascript and some HTML: <script type="text/javascript"> function hide(menu){ var menuStyle=document.getElementById(menu).style; menuStyle.display="none"; } function show(menu) { var menuStyle=document.getElementById(menu).style; menuStyle.display="block"; } </script> This Javascript code defines two functions:
The web page then needs two more things:
Something like: <div onmouseover="javascript:show('Menu1')" onmouseout="javascript:hide('Menu1')"> <b>Show Menu</b> <div style="display: none;" id="Menu1"> <a href="http://www.suite101.com/">Suite101.com</a><br> <a href="http://www.suite101.com/profile.cfm/linuxtalk">About</a> </div> In this example the text "Show Menu" will be displayed, and as soon as the user placed their mouse pointer over this text then the menu will be displayed. Finally the page needs some more Javascript - this time to hide the "menu" when the page is loaded: <script type="text/javascript"> hide('Menu1'); </script> A Complete Javascript MenuThe only differences between the simple example shown above and a complete menu are:
The additional style information does the following jobs:
And so the source for a web page containing a menu is: <html> <head> <style> .menu1,.submenu1 {background-color:silver; position:absolute; left:0; top:0; width:50; z-index:1;} .submenu1 { z-index:0; top:20; width:100; } .menu2,.submenu2 {background-color:lightgreen; position:absolute; top:0; left:50; width:50; z-index:1; } .submenu2 { z-index:0; left:0; top:20; width:300; } </style> <script type="text/javascript"> function hide(menu){ var menuStyle=document.getElementById(menu).style; menuStyle.display="none"; } function show(menu) { var menuStyle=document.getElementById(menu).style; menuStyle.display="block"; } </script> </head> <body> <div> <table cellspacing=0 cellpadding=2> <tr> <td onmouseover="javascript:show('Menu1')" onmouseout="javascript:hide('Menu1')" valign=top class=menu1>Home <div id=Menu1 class=submenu1> <a href=http://www.suite101.com>Suite101.com</a><br> <a href=http://www.suite101.com/profile.cfm/linuxtalk>About</a> </div> </td> <td onmouseover="javascript:show('Menu2')" onmouseout="javascript:hide('Menu2')" valign=top class=menu2>Articles <div id=Menu2 class=submenu2> <a href=http://database-programming.suite101.com/article.cfm/mysql_stored_procedures_and_functions> MySQL Stored Procedures and Functions</a><br> <a href=http://computer-programming-languages.suite101.com/article.cfm/gambas_almost_visual_basic_on_linux> Gambas: Almost Visual Basic on Linux</a> </div> </td> </tr> </table> </div> <div style="position:absolute;top:30"> How to create a simple, yet effective, menu for a web page. </div> <script type="text/javascript"> hide('Menu1'); hide('Menu2'); </script> </body> </html> ConclusionA Javacript menu is a very simple yet effective technique - with it a web site developer can produce professional looking menus with the minimum of time and effort
The copyright of the article Create a Javascript Menu in Javascript/Java Programming is owned by Mark Alexander Bain. Permission to republish Create a Javascript Menu in print or online must be granted by the author in writing.
Comments
Sep 18, 2008 9:54 AM
Guest
:
1 Comment:
|
|
|
|