David wrote:
Hi everyone,
Hoping there are some .js/browser experts out there that can help with this
weird problem.
The problem.
This all works fine in IE6, Opera7, NN4 but in Mozilla browsers ( NN7 -
Firefox 0.8 ) it is slow. It gets even slower if the closer image is made
taller. I don't understand this.
Works fine in Firefox 0.9.3 and Mozilla 1.7.2. It might just be a bug in earlier
Gecko-based browsers you will be unable to avoid.
Here is a link to see what is going on
http://mysite.verizon.net/res8xvny/menu/
Anyone have an idea what could be causing this? There are no animations on
the divs, just show hides. I have placed some comments in the code but it is
pretty simple to see what is going on in the function.
This function:
function Menu(subToShow,num){
var z,y,activeDrop,closer,layers,h="hidden",v="visible ";
if((y=MM_findObj("drop0"))!=null){closer=(document .layers)?y:y.style;} //
closer div.
if((z=MM_findObj(subToShow))!=null){activeDrop=(do cument.layers)?z:z.style} //
the active drop div.
for(i=0;i<20;i++){
if((x=MM_findObj("drop"+[i]))!=null){subArray=(document.layers)?x:x.style;} //
all of the drop divs.
closer.visibility=v; // "show" the closer.
if(num>0){
subArray.visibility=h; // "hide" all the rest.
if(activeDrop){activeDrop.visibility=v} // "show" the drop.
}else{
closer.visibility=h; // "hide" the closer.
subArray.visibility=h; // "hide" all the rest.
}
}
}
is pretty badly written.
Every pass through the loop you are doing a '"show" the closer'. As well, every
pass through the loop you are doing a '"show" the drop'. These things only need
to happen once. The only reason you are looping is to '"hide" all the rest'.
This is probably the only thing you should be doing in the loop. The reason
you're setting the activeDrop on every pass is because you are turning
everything off unconditionally all the time, so at some point you end up turning
off the layer you want on. As well, you are checking for 20 layers regardless of
whether they actually exist or not. As well, you turn the closer on with each
pass through the loop, but if num is < 1 (whatever num is) then you turn it off
immediately. To correct at least some of these deficiencies I've modified the
original function:
function Menu(subToShow,num){
var z,y,h="hidden",v="visible";
if ((y = MM_findObj("drop0")) != null) {
var closer = (document.layers) ? y : y.style;
if (num > 0) {
closer.visibility = v;
} else {
closer.visibility = h;
}
}// closer div.
if ((z = MM_findObj(subToShow)) != null) {
var activeDrop = (document.layers) ? z : z.style;
if (activeDrop && num > 0) {
activeDrop.visibility = v;
}
} // the active drop div.
var i = 0;
while ((x = MM_findObj("drop" + i)) != null) {
if ("drop" + i != subToShow && num > 0) {
subArray = (document.layers) ? x : x.style;
subArray.visibility = h;
}
i++;
}
}
Untested and it doesn't improve things very much. But it does stop you from
setting the visibility of closer and activeDrop 20 times everytime you mouseover
something, which is probably the problem.
--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ -
http://jibbering.com/faq