crossbrowser reusable javascript for dropdown that hides another menu when active?
October 31, 2006 6:57 PM
Subscribe
What I suspect is a fairly stupid javascript question, but one I haven't found an ideal solution for. I four menus made out of unordered lists. I'm using a modified version of the suckerfish dropdown script to make it work in IE (code inside), in one place I want 1 of the four menus to stay open except when I hover on one of the remaining menus. i.e. menu1 is visible. when I mouseover menu2, menu1 is hidden. When I mouseout of menu2 menu1 reappears.
I built the menu as four separate lists rather than containing them in a parent list as I normally would because of some tricky positioning requirements that were generating weird effects in IE6 and that, when fixed, made the menus work rather poorly. My current approach doesn't require any css hacks, either, which is a good thing now that IE7 is here. Here is the code to make the menus work in IE:
startList = function() {
if (document.all && document.getElementById) {
for (d = 1; d< 10; d++) { navroot=document.getElementById('menu'+d); for (i=0; inavroot.childnodes.length; i++) { node=navRoot.childNodes[i]; if (node.nodename="LI" ) { node.onmouseover=function() { this.classname+="over" ; if (d!=1) menu1.classname+="hide" ; } node.onmouseout=function() { this.classname=this.className.replace(" over" , ); menu1.classname=menu1.className.replace(" hide" , ); } } } } } } /code>
Those familiar with the sfhover javascript will notice the only difference in mine is for (d = 1; d< 10; d++) {/code> Of course, this only works in IE. What I need is something that works in all browsers and doesn't require adding any javascript anywhere in the body. What it should do is add a class called 'hide' to the LI in menu1 whenever the user mouses over menu2 and remove the class 'hide' from menu1 when the user mouses out. It has to work in IE6 , IE7, Firefox, and Safari. It has to not interfere with the dropdown script that is required since IE won't recognize :hover on elements other than anchors.
While I'm revealing the depths of ignorance, I'd really like to understand how to make this work. Thanks!
As a secondary, and far less important question, does anyone know where I can download IE6? I use Firefox and recently updated IE to 7 to mess around. I intended to install it alongside 6 so I could test layouts in both 6 and 7, but I seem to have uninstalled 6. Anyway. Back to the real question.>>
posted by Grod to computers & internet (6 comments total)
startList = function() {
if (document.all && document.getElementById) {
for (d = 1; d< 10; d++) {br> navRoot = document.getElementById('menu'+d);
for (i=0; i
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+="over";
if (d!=1) menu1.className+="hide";
}
node.onmouseout=function() {
this.className=this.className.replace("over", "");
menu1.className=menu1.className.replace("hide", "");
}
}
}
}
}
}
window.onload=startList;
Those familiar with the sfhover javascript will notice the only difference in mine is for (d = 1; d< 10; d++) {/code> Of course, this only works in IE. What I need is something that works in all browsers and doesn't require adding any javascript anywhere in the body. What it should do is add a class called 'hide' to the LI in menu1 whenever the user mouses over menu2 and remove the class 'hide' from menu1 when the user mouses out. It has to work in IE6 , IE7, Firefox, and Safari. It has to not interfere with the dropdown script that is required since IE won't recognize :hover on elements other than anchors.
While I'm revealing the depths of ignorance, I'd really like to understand how to make this work. Thanks!>
posted by Grod at 8:00 PM on October 31, 2006