|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Editions | myCNN | Video | Audio | Headline News Brief | Feedback | ![]() |
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Rollover effects made easy
(IDG) -- One of the first exploitations of JavaScript was to change images when users rolled a mouse over them. This change simulated the actions existing in common applications, such as the browsers themselves, where rolling over a button shows a raised button. Creating rollovers is easy because they don't involve a lot of effort or code, and they don't consume a lot of time. Before you start creating any rollover effects, though, you should understand the different kinds. Webmasters often think JavaScript must be used, but with the release of Cascading Style Sheets (CSS), other methods are now supported in some browsers. The following list outlines the three methods I consider to be the most prevalent:
Because the first two require JavaScript, I'll begin by focusing on the JavaScript events you must use to trigger the rollovers. It is these events that allow you to replace images, as well as manipulate layers. JavaScript eventsThe events you'll be using are in the following list with a brief description. This column assumes you're familiar with JavaScript; if you're not, you may wish to visit your local bookstore or do some research on the Web.
As you can see, we're focusing only on the two core events in JavaScript to create rollover effects. As the various levels of the Document Object Model (DOM) become implemented in browsers, we may see additional events and handlers, or even new functions that allow for easier creation of rollovers. Keep an eye out for new standards and enhancements in browsers. You can also use the Image rolloversThe first type of rollover we're going to tackle is the common image rollover. Several methods of implementing this functionality exist, but the easiest involves preloading all the images in an instance of the Array object, then using a common function to swap them out. When a rollover or rollout event is fired, the corresponding handler captures the event and calls the function, which replaces the image. The first step is to build the necessary Array instances to hold the images. Because the image can be displayed in one of two states (rolled over or not), we'll need two arrays, which can be created with the following code.
// Create arrays As you can see, the first two positions in the array instances are filled with Image instances (passing height and width). I have named the arrays origImg (original images displayed) and overImg (images that represent the rolled-over state) and specified the URL location of the image to set the src property. I also specified "home" and "news" buttons, which you might see on any site. This example is an actual URL, so I'm assuming home and news are in the same directory as the HTML file loading them. Be sure to append any hostname and/or path if you want to place them in another location. Once the arrays have been created and populated, we can build the function that will perform the swapping of images. Because we want to reuse the code for both states, we're going to design it to take two parameters. The first parameter is the indexed location of the image on the page, which will allow us to easily tell which image was rolled over and, therefore, which one to replace. The second parameter contains a string that tells the function what type of swap (i.e., "over" or "out") is occurring. However, before I define how we're going to accomplish this task, we should focus on what's going to happen in the browser. After the page loads, users should be able to roll their mouse over one of two images on the page. When this is done, the proper event (again, Inside the function we will use a switch statement to determine the type of replacement we need to perform. If the function was passed the string over, we'll pull the image from the overImg array instance. If the function was passed ou", we'll pull the image from the origImg array instance. Inside the switch statement itself, we'll use the src property in conjunction with the indexed location of the image (remember, it was passed as a parameter) to replace the image. A couple of things should be noted. First, the image's indexed location on any given page starts with 0, not 1. This is also true for arrays. Second, the indexed position where we loaded our images corresponds with the location on the page. So, if the first image on the page is the home button, which it will be, its indexed position is 0. In loading our array with images, notice that the 0 position in the overImg and origImg arrays have the over and original home images referenced. Be sure to line these up, otherwise you may swap the wrong images out. Here is the function we will use:
Finally, we must add the links and images to the HTML page. This is simple HTML -- an <a> tag surrounding an <img> tag. I showed you the syntax for adding the event handlers to the <a> tag earlier in the column, so let's glance at what it will look like with the calls added to our function. The following example indicates how to use these event handlers to call the function and pass the proper parameters.
And that's it. I have included the entire source if you want to give it a try. Be sure to create some images to load and name them correctly. You will see in the source the entire process of creating the arrays, preloading the images, defining the function, and including the images. I used a <table> to control the spacing of the images, but that isn't necessary for the rollover to work.
Follow this link to read the rest of the article detailing Layer rollovers & CSS rollovers RELATED STORIES: Wireless Application Protocol draws criticism RELATED IDG.net STORIES: The basics of Java platform performance RELATED SITES: Examples of rollovers on Netscape's DevEdge site | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Back to the top |
© 2001 Cable News Network. All Rights Reserved. Terms under which this service is provided to you. Read our privacy guidelines. |