This file holds
- the functions and the variables to show and hide the layers and
- the functions to change the roll-over images.
Only the former are relevant for the dynamic menus but the latter add an often appreciated - and now common - feature of image links.
The file begins with the definition of an Boolean array, with one entry per menu. I defined the menus by numbers, from 0 to 4, from left to right and the array follows the same sequence. If one layer is visible, the appropriate array entry is true, otherwise it is false.
Then, there is the definition of a time-out array. If the layer's visibility is dependent on an impending time-out, its ID is stored in the appropriate entry of the array. Otherwise the array value is meaningless. In this tutorial I use 5 menus, hence the number five. For the sake of generality, you should use a constant with the number of menus.
There are only to actions applied to the menus: show and hide, corresponding to the functions showMenu(idx) and hideMenu(idx).
The showMenu(idx) function receives the index of the menu (a whole number defined by the user) and sweeps all layers in a loop to check if any layer, different from
idx
is visible (the
dynmenu[i]
entry is "true"). If this occurs, the layer is hidden:
for(i=0;i<5;i++) if (dynmenu[i] && (i!=idx)) hideMenu(i);
The next action is to change layer visibility to "visible". However, the code must be forked to meet the different implementations of layers by the various browsers:
if(document.getElementById) document.getElementById("dynmenu"+idx).style.visibility='visible'; else if (document.all) eval("document.all.dynmenu"+idx+".style.visibility='visible';"); else if (document.layers) eval("document.dynmenu"+idx+".visibility='visible';");
The first line is interpreted by newer browsers that adhere to the World Wide Web Consortium (W3C, www.w3c.org) Document Object Model specification. This is the case for Netscape Navigator 6 and later and Microsoft Internet Explorer 6 and later. The name of the "element" is composed by "dynmenu" plus the index "idx" and its property
.style.visibility
is set to "visible".
The second line is addressed to Microsoft Internet Explorer 4 and 5 releases. The name of the "element" is composed by "dynmenu" plus the index "idx" and its property
.style.visibility
is set to "visible". In this case, the layers are found under the
document.all
object array.
The third line is addressed to Netscape Navigator 4 release. The name of the "element" is composed by "document.dynmenu"+idx and its property
visibility
is set to "visible". In this case, the layers are found under the
document.layers
object array.
Then, the
dynmenu
array is updated, by setting the entry to true:
dynmenu[idx]=true;
Finally, a timer is set: if the user does not select one entry within 8000 milliseconds, the visible layer will be automatically hidden:
timerID[idx]=setTimeout("hideMenu("+idx+")",8000);
This feature helps novice users that are not comfortable with layers and want to view the page without being disturbed by superimposed content. This solution has a shortcoming, though: if the user keeps the pointer over the dynamic for longer than the specified time, the menu will disappear and the user will have to return to the top tab to show it again. Thus, the time-out should balance the two conflicting goals.
Other implementations could solve this issue by automatically hiding the menu when the pointer is not over it. However, I prefer this solution because it is simpler for novice users and experienced users click the desired menu entry at once without any disturbance.
The hideMenu(idx) function is a "mirror" of showMenu(idx). It clears the timeout set by showMenu(idx). If this timeout has already expired, the browser ignores the command:
clearTimeout(timerID[idx]);
Then, it sets the
visibility
property of the idx-layer to "hidden". Again, the code must be forked to accommodate the differences among browsers:
if(document.getElementById) document.getElementById("dynmenu"+idx).style.visibility='hidden'; else if (document.all) eval("document.all.dynmenu"+idx+".style.visibility='hidden';"); else if (document.layers) eval("document.dynmenu"+idx+".visibility='hidden';");
The first line is interpreted by the browsers that adhere to the World Wide Web Consortium Dynamic HTML specification.
The second line is addressed to Microsoft Internet Explorer 4 and 5 releases. In this case, the layers are found under the
document.all
object array.
The third line is addressed to Netscape Navigator 4 release. In this case, the layers are found under the
document.layers
object array.
Finally, the
dynmenu
array is updated, by setting the entry to false:
dynmenu[idx]=false;
The roll-over image effect is implemented with two functions: imgOver(imgHndl) and imgOut(imgHndl). The first is associated to the
onMouseOver
event while the second is associated to the
onMouseOut
event.
When the pointer (also known as the cursor) passes over an image with the roll-over the source of the image is changed to a filename equal to the current filename with the "ov" suffix: if the source of an image is the "XYZ.jpg" file, the source of the roll-over image is "XYZov.jpg". Thus, every roll-over image's source must be duplicated with a source with the "ov" suffix.
The imgOver() function code is explained below:
imgHndl.src=imgHndl.src.substr(0,imgHndl.src.length-4)+"ov"+imgHndl.src.substr(imgHndl.src.length-4);
- The image handler is passed to the
imgOver(imgHndl) function.
- The source property is extracted from the handler and put into a string.
- This string is split in two sub-strings. The first part runs from the beginning (offset set to 0) to the length of the string except the last four characters; these four characters are the second sub-string, running from this point (offset set to the length-4) to the end of the string and correspond to the image extension, usually ".jpg", ".jp2", ".gif" or ".png".
- The new image source is set by joining the first sub-string with the "ov" suffix with the extension.
When the pointer leaves an image with the roll-over the source of the image is restored to the original filename, without the "ov" suffix by means of the imgOut(imgHndl) function:
imgHndl.src=imgHndl.src.substr(0,imgHndl.src.length-6)+imgHndl.src.substr(imgHndl.src.length-4);
- The image handler is passed to the
imgOut(imgHndl) function.
- The source property is extracted from the handler and put into a string.
- This string is split in two sub-strings. The first part runs from the beginning (offset set to 0) to the length of the string except the last six characters; the second sub-string holds the extension (offset set to the length-4 and running to the end of string).
- The new image source is set by joining the first sub-string with the extension.
This code is general purpose and is independent of the paths of the images.
The only requirements are the use of the "ov" suffix and
the three letter extensions.
You should be aware that only the image's source is changed. The
size of the original image is kept, regardless of the actual size
of the roll-over image.
|