Magic Buttons - part 2

An important concept here is the fact that we can NAME (or label or mark) an image location. We have been using imgXX as the label.

If we were to switch things around from this...

<A HREF="target.html" onMouseOver="hiLite('img01','clickme2')" onMouseOut="hiLite('img01','clickme1')"><IMG NAME="img01" SRC="clickme1.gif" width=75 height=22 border=0></A>

<A HREF="target2.html" onMouseOver="hiLite('img02','clickme4')" onMouseOut="hiLite('img02','clickme3')"><IMG NAME="img02" SRC="clickme3.gif" width=102 height=22 border=0></A>

to this...

<A HREF="target.html" onMouseOver="hiLite('img02','clickme4')" onMouseOut="hiLite('img02','clickme3')"><IMG NAME="img01" SRC="clickme1.gif" width=75 height=22 border=0></A>

<A HREF="target2.html" onMouseOver="hiLite('img01','clickme2')" onMouseOut="hiLite('img01','clickme1')"><IMG NAME="img02" SRC="clickme3.gif" width=102 height=22 border=0></A>

...we get a different effect entirely.

VIEW IT

As a matter of fact, using this method, you can cause effects in any image(s) from any other image(s). Alls ya gotta do is keep the variables straight. Here's another example. It is identical to the last one except it also activates a third image.

Here are the images...
Save this as camera_1.gif and this as camera_2.gif.

And here is the coding...

<HTML>

<HEAD>
<TITLE>Magic Buttons!</TITLE>

<SCRIPT LANGUAGE="JavaScript">
<!--- hide script from old browsers

// browser test:
browserName = navigator.appName;
browserVer = parseInt(navigator.appVersion);
if (browserName == "Netscape" && browserVer >= 3) version = "1";
else version = "2";

// preload images:
if (version == "1") {

clickme1 = new Image(75,22);
clickme1.src = "clickme1.gif";

clickme2 = new Image(75,22);
clickme2.src = "clickme2.gif";

clickme3 = new Image(102,22);
clickme3.src = "clickme3.gif";

clickme4 = new Image(102,22);
clickme4.src = "clickme4.gif";

camera_1 = new Image(42,44);
camera_1.src = "camera_1.gif";

camera_2 = new Image(42,44);
camera_2.src = "camera_2.gif";

}

function hiLite(imgDocID,imgObjName) {
if (version == "1") {
document.images[imgDocID].src = eval(imgObjName + ".src")
}}

// end hiding --->
</SCRIPT>

</HEAD>

<BODY BGCOLOR="#FFFFFF">

<A HREF="target.html" onMouseOver="hiLite('img02','clickme4');hiLite('img03','camera_2')" onMouseOut="hiLite('img02','clickme3');hiLite('img03','camera_1')"><IMG NAME="img01" SRC="clickme1.gif" width=75 height=22 border=0></A>

<A HREF="target2.html" onMouseOver="hiLite('img01','clickme2');hiLite('img03','camera_2')" onMouseOut="hiLite('img01','clickme1');hiLite('img03','camera_1')"><IMG NAME="img02" SRC="clickme3.gif" width=102 height=22 border=0></A>

<IMG NAME="img03" SRC="camera_1.gif" width=42 height=44 border=0>

</BODY>

</HTML>

VIEW IT

So, as you can see, this technique offers quite a few possibilities. Also, you can put these buttons in a table or embed them in a form. The most important thing is to keep careful track of the variables.


Now we'll touch on a few basic button styles. The most basic is a simple color change. All I did was lighten the button for the "on" effect.

       

       
* All button images can easily be grabbed off the bottom of this page.

This technique kinda sorta makes a button "pop up".

       

And this is my favorite. A true "button". The second image is made by moving the contents of the first image one pixel down and one pixel to the right, and altering the edge treatment a bit. You can do this easily (once you get the hang of it) in most any graphics editor.

       

You can put these guys in a table for nifty button bar effect.

There is one more thing we can add. And that is the "onClick" function. In addition to highlighting the button when the cursor passes over it, you can make a button depress when someone clicks on it. The only problem is, I'm not too happy with the way the effect is rendered (the button seems to depress on mouse up rather than mouse down). Here is an example followed by the coding. You can decide for yourself whether or not you like it.

       

<A HREF="examples/target.html" onMouseOver="hiLite('img10','cbutton4')" onMouseOut="hiLite('img10','cbutton1')" onClick="hiLite('img10','cbutton7')"><IMG NAME="img10" SRC="cbutton1.gif" width=116 height=36 border=0></A>
&nbsp;&nbsp;&nbsp;
<A HREF="examples/target.html" onMouseOver="hiLite('img11','cbutton5')" onMouseOut="hiLite('img11','cbutton2')" onClick="hiLite('img11','cbutton8')"><IMG NAME="img11" SRC="cbutton2.gif" width=116 height=36 border=0></A>
&nbsp;&nbsp;&nbsp;
<A HREF="examples/target.html" onMouseOver="hiLite('img12','cbutton6')" onMouseOut="hiLite('img12','cbutton3')" onClick="hiLite('img12','cbutton9')"><IMG NAME="img12" SRC="cbutton3.gif" width=116 height=36 border=0></A>

Couple notes:
  1- Don't forget to add the stuff in the HEAD tags.
  2- I pulled this coding from this page so the imgXX references and the target reflect that.
  3- &nbsp; is the code for a blank space. I used it to separate the buttons a little.

This is another effect that you can make. Moving the cusor over each of the links moves the bullet to that link. This is an example of a mouseOver that alters a second image... very similar to the effect with the camera a ways back. The whole thing is held together in a table. The "bullet" effect consists of 2 images, the diamond and a empty white rectangle the same size. The menu on the right is the same thing but with the table borders turned on. If you need to brush up on HTML tables then have a look at Table Tutor.

             

Remember- when you're making your button images, try to keep it simple. If your images are large and complex the browser will lag trying to display them. If you keep them small, and just a few colors, they'll work like a snap. (Also, in case you were wondering, you can use jpg images as magic buttons.)

Well, that about does it for Magic Buttons. There is a whole lot more to learn about JavaScript and I encourage you to have a look through the JavaScript resources that we've provided.

Shown below are all the buttons used in the examples (because sometimes they're kinda hard to grab). If you make buttons that can be used in the JavaScript presented here and you want to share them with the world, zip them up and send them to me (include your homepage URL). I'll post them here with a credit.

    
    
















--> <-- (don't forget the blank image)

To start of Magic Buttons Tutorial


PROFESSIONAL WEB DESIGN