Forum Moderators: open
Thanks for any help you can offer...
First, create the menu you want to appear, e.g.
<ul class=submenu id=widgets onmouseover="showMenu('widgets'); onmouseout="hideMenu('widgets')">
<li><a href=greenwidgets.html>Green Widgets</a></li>
<li><a href=bluewidgets.html>Blue Widgets</a></li>
<li><a href=redwidgets.html>Red Widgets</a></li>
</ul>
It might seem odd that we call showMenu() when we mouseover this menu, since it's already supposed to be visible once we've moused over the menu bar. But trust me, you have to have showMenu() in *both* locations or it doesn't work right.
Notice we assigned a class called "submenu" to the <ul>. That's so we can style it with CSS. One of the definitions will be to make it invisible, because we don't want it to show until there's a mouseover:
<style type=text/css>
.submenu { visibility:hidden; position: absolute}
</style>
We also set the position to absolute, which will let us move it around later.
Now you set a mouseover to call the menu:
<a href=widgetsummary.html onmouseover="showMenu('widgets')" id=widgetsLabel>Widgets</a>
Our menu links to a page called widgetsummary.html so that a click will still take the visitor to someplace useful even if they have Javascript turned off.
Now you need to write Javascript functions that position the menu in the right spot, and show & hide it as requested, like this:
function showMenu(whichMenu) {
setMenuLocation(whichMenu);
submenuObj=document.getElementById(whichMenu);
submenuObj.style.visibility = 'visible';
}
document.onclick=hideAllMenus;
function hideMenu(whichMenu) {
document.getElementById(whichMenu).style.visibility='hidden';
}
function setMenuLocation(whichMenu) {
menuObj = document.getElementById(whichMenu+'Label');
submenuObj=document.getElementById(whichMenu);
submenuObj.style.top = findTop(menuObj)+17;
submenuObj.style.left = findLeft(menuObj);
}
//----------------------------------------------------------------
function findLeft(obj) {
//----------------------------------------------------------------
curleft = 0;
if(obj.offsetParent)
while(1) {
curleft += obj.offsetLeft;
if(!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if(obj.x)
curleft += obj.x;
return curleft;
}
//----------------------------------------------------------------
function findTop(obj) {
//----------------------------------------------------------------
curtop = 0;
if(obj.offsetParent)
while(1) {
curtop += obj.offsetTop;
if(!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if(obj.y)
curtop += obj.y;
return curtop;
}
JavaScript rollovers are a pet peeve of mine (and of quite a few usability people). It drives me absolutely stark raving mad to have the page become obscured because I happened to move the mouse near where I was reading, and a $#@! rollover obscured the exact text I was trying to read.
Actually...it's not much of a drive. More of a short putt. ;)
If you must use drop-down menus, Google for Suckerfish - those use a minimal amount of Javascript and mostly rely on CSS to do the work.
How about a 100% CSS solution [cssplay.co.uk]?