Sams Teach Yourself JavaScript in 24 Hours Free Open Book

Sams Teach Yourself JavaScript in 24 Hours

Previous Page
Next Page

Creating Rollovers

One of the classic uses of JavaScript is to create rolloversimages that change when you move the mouse over them. They are typically used to create navigation links that give the user a bit of guidance by highlighting the one the mouse is over.

In this section, you'll learn how to use JavaScript's dynamic images to create rolloversand then you'll learn why you shouldn't do this most of the time, and how to create rollovers with no scripting at all.

JavaScript Rollovers

First, let's take a quick look at how to create rollovers using JavaScript. To do this, you start with regular and highlighted versions of each rollover image. Figure 19.1 shows two examples of navigation buttons in both states.

Figure 19.1. Regular and highlighted versions of two button images.


As you might guess, all this requires in JavaScript is to combine an onMouseOver event handler with a dynamic image. Adding onMouseOut allows your script to restore the original image when the mouse moves away. Listing 19.1 shows a simple way to do this with inline event handlers.

Listing 19.1. Using Basic JavaScript Rollovers

<html>
<head>
<title>Rollovers - JavaScript</title>
</head>
<body>
<h1>JavaScript Rollovers</h1>
<a href="home.html"
   onmouseover="document.images[0].src='home2.gif';"
   onmouseout="document.images[0].src='home1.gif';">
   <image border="0" src="home1.gif">
</a>
<br>
<a href="archives.html"
   onmouseover="document.images[1].src='archives2.gif';"
   onmouseout="document.images[1].src='archives1.gif';">
   <image border="0" src="archives1.gif">
</a>
</body>
</html>

This is just a basic bit of inline JavaScript, so you can test it by simply loading the HTML file into a browser. The results are shown in Figure 19.2. In the figure, the mouse cursor is over the Archives button.

Figure 19.2. The rollover example in action.


CSS Rollovers Without JavaScript

Although JavaScript rollovers work fine in today's browsers, the technique was developed in the days before CSS, and there is now a better way to accomplish the same thing. Using the :hover directive in CSS, you can create text links that change color when the mouse hovers over them. Listing 19.2 shows a simple example of CSS rollovers.

Listing 19.2. JavaScript-Free Rollovers with CSS

<html>
<head>
<title>Rollovers - CSS</title>
<style>
#home,#archives {
   font-size: 30px;
   text-decoration: none;
}
#home:hover, #archives:hover {
   background-color: #AAAAAA;
}
</style>
</head>
<body>
<h1>JavaScript-Free Rollovers</h1>
<a id="home" href="home.html"><b>Home</b></a>
<br>
<a id="archives" href="archives.html"><b>Archives</b></a>
</body>
</html>

To try this example, simply load the HTML document into a browser. When you move the mouse over the links, their background color changes from white to gray. This example is shown in Figure 19.3.

Figure 19.3. Simple CSS-only rollovers.


This isn't as fancy as the JavaScript rollovers, but it has some advantagesfirst of all, it doesn't require JavaScript. Second, the links are actual textthis means they'll work even in text-based browsers, primitive mobile phone browsers, and voice-reading browsers for the blind, although the rollover effects won't work in these situations. Third, the page loads faster, and you can add more links without creating graphics.

Graphic CSS Rollovers

Suppose you're really attached to the nifty graphic look of the first rollover example. Before you do something like that, take a look at Listing 19.3. This listing uses CSS to implement graphic rollovers, which look and work exactly like Figure 19.2, with no JavaScript.

Listing 19.3. Graphic Rollovers with CSS

<html>
<head>
<title>Rollovers - CSS</title>
<style>
#home {
   display: block;
   height: 60px;
   width: 126px;
   background-image: url("home1.gif");
}
#home:hover {
   background-image: url("home2.gif");
}
#archives {
   display: block;
   height: 60px;
   width: 168px;
   background-image: url("archives1.gif");
}
#archives:hover {
   background-image: url("archives2.gif");
}
#home b, #archives b {
   display: none;
}
</style>
</head>
<body>
<h1>JavaScript-Free Rollovers</h1>
<a id="home" href="home.html"><b>Home</b></a>
<br>
<a id="archives" href="archives.html"><b>Archives</b></a>
</body>
</html>

Here's a summary of how the CSS works:

  • The #home and #archives rules, which match the id attribute of the two links, set their display attribute to block and the width and height attributes to allow the links to be as large as their corresponding graphics. They then use the background-image property to display the unhighlighted graphics (home1.gif and archives1.gif).

  • The #home:hover and #archives:hover rules change the background images to the highlighted versions (home2.gif and archives2.gif).

  • The #home b and #archives b rule hides the text of the links within the <b> tags. This prevents the text from appearing on top of the graphics.

Notice that the HTML portion of this example is identical to the previous example, and it will work exactly the same on text-based browsers and browsers with JavaScript turned off. Users with modern browsers will see the graphic versions of the links instead. This gives you the look of graphic rollovers without JavaScript, and without compromising accessibility.

By the Way

Another reason to use this type of rollover: Because the links are still in the HTML as text, search engines see them as ordinary links, and can do a better job of indexing your site. See Hour 15, "Unobtrusive Scripting," for more information on accessibility and search engine optimization.


Previous Page
Next Page
Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y]


     Main Menu
Sams Teach Yourself JavaScript in 24 Hours
Table of Contents
Copyright
About the Author
Acknowledgments
Part I: Introducing the Concept of Web scripting and the JavaScript Language
Part II: Learning JavaScript Basics
Part III: Learning More About the DOM
Part IV: Working with Advanced JavaScript Features
Part V: Building Multimedia Applications with JavaScript
Hour 19. Using Graphics and Animation
Using Dynamic Images
Creating Rollovers
A Simple JavaScript Slideshow
Summary
Q&A
Quiz Questions
Quiz Answers
Exercises
Hour 20. Working with Sound and Plug-Ins
Part VI: Creating Complex Scripts
Part VII: Appendixes
Index


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele