Step Five: Complete the Code
Let's review what we have done so far:
All that left to do now is to tweak the code. Granted, it's going to be a pretty big tweak, but Dreamweaver has really done all the hard work of inserting the layers and writing the cross-browser code for the Behaviors. Now, it's time to move to your favorite text editor and customize the HTML.
Cut and Paste the Function Calls
First, let's move the JavaScript function calls, onMouseOver and onMouseOut, away from the image and into each of the image map areas.
<a> tag.</a>
tag, following the <img> tag.<area> tag at the end and paste the function calls into
the tag.<area shape="circle" coords="300,98,56" href="nest.htm">
Now, it should read something like:
<area shape="circle" coords="300,98,56" href="nest.htm" onmouseout="MM_showHideLayers('document.layers[\'onLayer\']','document.all[\'onLayer\']','hide')"
onmouseover="MM_showHideLayers('document.layers[\'onLayer\']','document.all[\'onLayer\']','show')">
<area>
defined in your image map.Point to the Right Map
You won't be able to test the above changes until you make
one more minor change. Remember how, in Step 4, you created a temporary image
map to grab the clipping coordinates? Part of that process puts a usemap=mapname
attribute in the <img> tag of one
of the graphics. You now need to find that reference and change the usemap
value so that both images are pointing to our real image map.
Now when you test your page in a 4.0 browser, the image should only change when your pointer goes over the mapped areas. However, you'll notice that all of the image changes instead of just one area. It's now time to put that in place with the clipping function I mentioned earlier.
Build the Clipping Array
The next step involves taking the coordinates gathered from our pseudo-image map and plugging them into a JavaScript array. Because I kept the top and bottom coordinates constant, I only need to bring in the left and the right values. I also need an identifying number to mark each image map area.
In my example, I only had 3 image map buttons to include,
so you see only three setParams statements.
However, you could have as many buttons as you like.
The following code is included in the <script>
section, above the Macromedia functions.
arClips = new Array();
function setParams(the_clip,from,to) {
arClips[the_clip] = new Array();
arClips[the_clip][0] = from;
arClips[the_clip][1] = to
}
setParams(1,93,234);
setParams(2,236,365);
setParams(3,365,506);
clTop = 0;
clBot = 176;
The two variables, clTop and clBot, hold the values for the clip top and the clip bottom range, respectfully. Again, because these are constant, I can go ahead and set them.
Integrating the Code
Our last task is to tie all of our disparate parts together.
First we need to pass the identifying number from each image map area to the
function. I do this by just adding the number to the end of the each function
call in each <shape> tag. Where,
before the code read:
<area shape="rect" coords="376,36,486,141" href="slate.htm" onmouseout="MM_showHideLayers('document.layers[\'onLayer\']','document.all[\'onLayer\']','hide')"
onmouseover="MM_showHideLayers('document.layers[\'onLayer\']','document.all[\'onLayer\']','show')">
Now, it reads:
<area shape="rect" coords="376,36,486,141" href="slate.htm" onmouseout="MM_showHideLayersIM('document.layers[\'onLayer\']','document.all[\'onLayer\']','hide',3)"
onmouseover="MM_showHideLayersIM('document.layers[\'onLayer\']','document.all[\'onLayer\']','show',3)">
The additional number is bolded for easy reference. Be sure to use a unique number to identify each defined shape. I've also slightly modified the function name by adding "IM" for image map at the end. This is done to prevent Dreamweaver from overwriting the function should the same behavior be applied elsewhere on the page. Naturally, the name of the actual function needs to be modified as well.
Now, we need to modify the Dreamweaver generated function to read our new value and to act on our array. Listing 12-1 shows the basic Dreamweaver code for showing and hiding layers:
Listing 12-1: Basic Dreamweaver Function
function MM_showHideLayers() { //v1.2
var i, visStr, args, theObj;
args = MM_showHideLayers.arguments;
for (i=0; i<(args.length-2); i+=3) { //with arg triples (objNS,objIE,visStr)
visStr = args[i+2];
if (navigator.appName == 'Netscape' && document.layers != null) {
theObj = eval(args[i]);
if (theObj) theObj.visibility = visStr;
} else if (document.all != null) { //IE
if (visStr == 'show') visStr = 'visible'; //convert vals
if (visStr == 'hide') visStr = 'hidden';
theObj = eval(args[i+1]);
if (theObj) theObj.style.visibility = visStr;
} }
}
Modifying the code involves the following:
clRight
(Right Clip value), clLeft (Left Clip value) and the_clip
(the identifying number of the defined area).clLeft.clRight.You can see the changes, marked in bold, in Listing 12-2. Once you've implemented these changes, test your object. You should see the type of reaction demonstrated in Figure 12-8.
Caution
Make
sure that when you go to preview your work in a browser that the layer visibility
is set correctly for each layer.
Listing 12-2: Modified Image Map Rollover Function
function MM_showHideLayersIM() { //v1.2 Modified by jlowery
var i, visStr, args, theObj;
var clRight, clLeft, the_clip;
args = MM_showHideLayersIM.arguments;
for (i=0; i<(args.length-2); i+=4) { //with 4 args (objNS,objIE,visStr,the_clip)
visStr = args[i+2];
the_clip = args[i+3];
clLeft = arClips[the_clip][0];
clRight = arClips[the_clip][1];
if (navigator.appName == 'Netscape' && document.layers != null) {
document.onLayer.clip.left=clLeft
document.onLayer.clip.right=clRight;
theObj = eval(args[i]);
if (theObj) theObj.visibility = visStr;
} else if (document.all != null) { //IE
if (visStr == 'show') visStr = 'visible'; //convert vals
if (visStr == 'hide') visStr = 'hidden';
document.all.onLayer.style.clip ="rect(" + clTop + " " + clRight + " " + clBot + " " + clLeft + ")";
theObj = eval(args[i+1]);
if (theObj) theObj.style.visibility = visStr;
} }
}
Step Three: Attach the Behavior