Laying Out Popup Menus
Hopefully, the logic behind popup menus is straightforward. Making popup menus work practically in your layout can take some hard work.
Believe it or not, the best approach is actually a mathematical one, so get out your scratch paper before you start coding and make a few sketches and calculations.
Let's say you have a nav bar with five main categories. Let's say that you also know the nav-bar height is 19 pixels, and you want the nav bar to fit across an 800-width screen. Shave off 40 pixels for scrollbars and the like, and you have a nav bar of width 760. Divide it into five equal sections of 152 pixels each, as in Figure 10.5.
Figure 10.5. Begin by sketching your rough nav-bar design and calculating the size of each table cell.
[View full size image]

TIP
For these mathematics to add up the same way in Netscape and IE, be sure to set the following spacing attributes to 0: leftmargin, topmargin, marginwidth, and marginheight in the body tag; and border, cellpadding, and cellspacing in each table tag.
Now for the layers. You know how wide each layer has to be: 152 pixels, just like the width of each table cell in the nav bar. You need to calculate the height of each layer, which depends on how many navigation choices you want it to have. Obviously, this number can vary from menu to menu. Since the nav bar is 19 pixels tall, make it easy on yourself and take 19 pixels as your default height per menu choice. A menu with three choices only needs to be 57 pixels tall (19 times 3), while a menu with seven choices needs to be 19 times 7, or 133 pixels tall.
To line up the layers with the table cells, you express the location of each layer in offsets from the top and left sides of the browser window. Therefore, if your nav bar is 19 pixels tall, you want to position your layers vertically at 19 pixels from the top of the screen. Horizontally, you start at 0 (no offset from the left), and you position each new layer in increments of 152, as Figure 10.6 shows.
Figure 10.6. Position the layers in offsets from the top and left sides of the browser window.

TIPIf you use images instead of text links inside your popup menus, be sure to build extra whitespace into the design of your graphics. This helps to make your menus feel less cramped. |
Finally, drop a table for the menu into each layer, using one row per menu item, as in Figure 10.7.
Figure 10.7. Put a table inside each layer for the menus. Each menu item gets its own row in the table.

[View full width]
<html>
<head>
<title>Popup Menus</title>
<script language="JavaScript">
function doShowHide(popupMenu) {
/* This function launches when the visitor mouses over the
if (popupMenu == "menu1") {
document.getElementById("menu1").style.visibility="visible";
document.getElementById("menu2").style.visibility="hidden";
document.getElementById("menu3").style.visibility="hidden";
document.getElementById("menu4").style.visibility="hidden";
document.getElementById("menu5").style.visibility="hidden";
}
if (popupMenu == "menu2") {
document.getElementById("menu1").style.visibility="hidden";
document.getElementById("menu2").style.visibility="visible";
document.getElementById("menu3").style.visibility="hidden";
document.getElementById("menu4").style.visibility="hidden";
document.getElementById("menu5").style.visibility="hidden";
}
if (popupMenu == "menu3") {
document.getElementById("menu1").style.visibility="hidden";
document.getElementById("menu2").style.visibility="hidden";
document.getElementById("menu3").style.visibility="visible";
document.getElementById("menu4").style.visibility="hidden";
document.getElementById("menu5").style.visibility="hidden";
}
if (popupMenu == "menu4") {
document.getElementById("menu1").style.visibility="hidden";
document.getElementById("menu2").style.visibility="hidden";
document.getElementById("menu3").style.visibility="hidden";
document.getElementById("menu4").style.visibility="visible";
document.getElementById("menu5").style.visibility="hidden";
}
if (popupMenu == "menu5") {
document.getElementById("menu1").style.visibility="hidden";
document.getElementById("menu2").style.visibility="hidden";
document.getElementById("menu3").style.visibility="hidden";
document.getElementById("menu4").style.visibility="hidden";
document.getElementById("menu5").style.visibility="visible";
}
}
function doHide(popupMenu) {
/* This function launches when the visitor clicks the Close
document.getElementById(popupMenu).style.visibility="hidden";
}
</script>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<! First thing's first. Create the code for the popup menus.
Looking at the div tag, the id property gives the ID of the layer
The width and height give the physical dimensions of the layer.
Notice that the visibility is set to hidden, which makes the
<div id="menu1" style="position: absolute; left: 0px; top: 19px;
<table width="152" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><a href=" class="docEmphStrong">path01path02path03path04path05">Subcategory 5</a></td>
</tr>
<! This row contains the Close option at the bottom of the
<tr>
<td><a href="'menu1');">Close</a></td>
</tr>
</table>
</div>
<! This div element contains the second popup menu. It works
<div id="menu2" style="position: absolute; left: 152px; top:
<table width="152" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><a href=" class="docEmphStrong">path06path07path08path09path10">Subcategory 5</a></td>
</tr>
<tr>
<td><a href="'menu2');">Close</a></td>
</tr>
</table>
</div>
<! This div element contains the third popup menu. >
<div id="menu3" style="position: absolute; left: 304px; top:
<table width="152" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><a href=" class="docEmphStrong">path11path12path13path14path15">Subcategory 5</a></td>
</tr>
<tr>
<td><a href="'menu3');">Close</a></td>
</tr>
</table>
</div>
<! This div element contains the fourth popup menu. >
<div id="menu4" style="position: absolute; left: 456px; top:
<table width="152" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><a href=" class="docEmphStrong">path16path17path18path19path20">Subcategory 5</a></td>
</tr>
<tr>
<td><a href="'menu4');">Close</a></td>
</tr>
</table>
</div>
<! This div element contains the fifth popup menu. >
<div id="menu5" style="position: absolute; left: 608px; top:
<table width="152" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><a href=" class="docEmphStrong">path21path22path23path24path25">Subcategory 5</a></td>
</tr>
<tr>
<td><a href="'menu5');">Close</a></td>
</tr>
</table>
</div>
<! Here at last is the table that creates the nav bar. It's
<table width="760" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="152">
<! The onMouseOver event fires when the visitor moves the
<a href=" class="docEmphStrong">path26path27path28path29path30" onMouseOver="doShowHide('menu5');">
Category 5
</a>
</td>
</tr>
</table>
</body>
</html>
Figure 10.8. This Toolkit creates a system of popup menus.
[View full size image]

Figure 10.9. Substitute the text links with your own images (adding plenty of vertical whitespace), and the menus don't feel as cramped.
[View full size image]
