Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Ajax and JavaScript

  • Print
  • + Share This
This chapter is from the book

Building Three-State Rollovers

A three-state rollover is one where the rollover has three versions. Besides the original image and the version that appears when the user places the cursor over the image, there is a third version of the image when the button itself is clicked, as shown in Figure 4.5.

Figure 4.5

Figure 4.5 When the button is clicked, you get a third image (hard to see in this grayscale image; check our companion Web site for the full effect).

Script 4.4, the HTML file, looks almost exactly the same as Script 4.2 from the previous task. In fact, the only differences are the document’s title and the name of the external JavaScript file that is being called. That’s it. This is an example of why putting all your JavaScript into an external file is so powerful; you can add functionality to your pages without having to rework your HTML pages.

Script 4.4. By putting your JavaScript in an external file, the HTML for a three-state rollover is virtually identical to a two-state rollover.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/
        → xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
     <title>Three-state Rollovers</title>
     <script type="text/javascript"
     → src="script03.js"></script>
</head>
<body bgcolor="#FFFFFF">
     <a href="next1.html"><img src="images/
     → button1_off.gif" width="113" height="33"
     → border="0" alt="button1" id="button1"
     → /></a>&nbsp;&nbsp;
     <a href="next2.html"><img src="images/
     → button2_off.gif" width="113" height="33"
     → border="0" alt="button2" id="button2"
     → /></a>
</body>
</html>

In Script 4.5, the external JavaScript file, there are only a few changes from Script 4.3. Rather than go through the whole script again, we’ll just focus on the changes. Remember, the parts of the script that we’re covering are shown in red in the code.

To build a three-state rollover:

  1. thisImage.clickImage = new Image();
    thisImage.clickImage.src = "images/"
    → + thisImage.id + "_click.gif";

    In the setupRollover() function, we now need to add a third image property for the click state. In the first line, we create a new image object that will contain the clickImage version of the image. The second line sets the source for clickImage. It builds the name of the source file on the fly, concatenating "images/" with the id of the image, and adding "_click.gif".

  2. thisImage.onclick = function() {
      this.src = this.clickImage.src;
    }

    This tells the browser what to do when the user clicks the mouse on the image: in this case, we want to set the image source to its clickImage version.

    Script 4.5. This script powers the three-state rollover.

    window.onload = rolloverInit;
    
    function rolloverInit() {
         for (var i=0; i<document.images.length;
         → i++) {
            if (document.images[i].parentNode.
            → tagName == "A") {
               setupRollover(document.images[i]);
            }
         }
    }
    
    function setupRollover(thisImage) {
         thisImage.outImage = new Image();
         thisImage.outImage.src = thisImage.src;
         thisImage.onmouseout = function() {
            this.src = this.outImage.src;
         }
    
         thisImage.clickImage = new Image();
             thisImage.clickImage.src = "images/" +thisImage.id + "_click.gif";
             thisImage.onclick = function() {
                this.src = this.clickImage.src;
             }
    
         thisImage.overImage = new Image();
         thisImage.overImage.src = "images/" +
         → thisImage.id + "_on.gif";
         thisImage.onmouseover = function() {
            this.src = this.overImage.src;
         }
    }
  • + Share This
  • 🔖 Save To Your Account