Cd LOGO
It appears that you are using Internet Explorer to browse. As a result you may find some of the pages here on ERT and many other sites do not perform or render well. Over 60% of ERT visitors are using Firefox, so we optimize for them. There is a long technical reason why Internet Explorer does not work well but in simple terms Internet Explorer is a tired relic of 20th century technology. Perhaps IE7 will turn out to be a true upgrade and not a new coat of paint on the same shortcomings. We recommend that you install Firefox or another Standards compliant browser to maximize your access and enjoyment on the Internet. If you must use IE, setting the caching to automatic may help with some of the issues it will have with ERT pages.

a tutorial by COBOLdinosaur

One of the most common effects used on web pages is the image rollover. There are two problems associated with the effect:

First. They require a pre-load to work smoothly. That means scripting must be enabled, and each image must be separately addressed.

Secondly. The increase response times because of extra graphics have to be downloaded. Even if the graphics are small; there is still a separate request and disk I/O on the server 10 graphics that are 3k takes longer than 1 graphic that is 30k

What we are going to look at here is using a single large image created from smaller ones and using the CSS background properties to manipulate that image to give us different presentations of it, for rollover effects. We will address both the problems that image rollovers create:

  1. Elimination of the preload, and removal of the scripting to do it.
  2. Reducing the images to a single background image that can be used for any element.

For the initial demonstrations we will use this simple image:

It was created by combining two background textures in a graphics editor. You can use any graphic, and as much detail as you want and whatever editor. The detail and quality of the image, and the tool that creates it make no difference to how the effects will work. You just need to record where the boundaries are between the image components so you will know how to set the CSS values.


To start let's just set up the background on a couple of div tags to use the two parts of the image. Here is the CSS:

#div1 {height:45px;
       width:108px;
       text-align:center;
       padding-top:10px;
       color:white;
       display:block;
       background: url("dual.gif") -2px -2px  no-repeat;
	}
#div2 {height:45px;
       width:108px;
       text-align:center;
       padding-top:10px;
       color:white;
       display:block;
       background: url("dual.gif") -116px -2px  no-repeat;
	}
and the divs display as:
Div1


Div2


Whoopee! So What?


The next step is to set up the same CSS for the states of our A tags:

a {height:45px;
       width:108px;
       text-align:center;
       padding-top:10px;
       color:white;
       display:block;
       background: url("dual.gif") -2px -2px  no-repeat;
	}
a:hover {height:45px;
       width:108px;
       text-align:center;
       padding-top:10px;
       color:white;
       display:block;
       background: url("dual.gif") -116px -2px  no-repeat;
	}
Can you see what that is going to do without even looking at the example? This is what happens: Anchor
AHA! an image rollover, but only one image, and it is the background, so the text link can go on top of it and it is not necessary to have a separate pair of images for each link. That does open up quite a number of possibilities. Like this!

Those are using simple images, but virtually any image; and any image size can be used this way.


Of course, hover of other elements is pretty limited and we may want to do the rollovers using mouse events. It's really really easy. Start with this CSS for buttons:

.bcls1 {background: url("dual.gif") -2px -2px  no-repeat;
        color:yellow;
       }
.bcls2 {background: url("dual.gif") -116px -2px  no-repeat;
        color:yellow;
       }
And code the buttons this way:
<input type="button" class="bcls1" value="Button 1"
       onmouseover="this.className='bcls2'" 
       onmouseout="this.className='bcls1'">
<input type="button" class="bcls1" value="Button 2"
       onmouseover="this.className='bcls2'" 
       onmouseout="this.className='bcls1'">
<input type="button" class="bcls1" value="Button 3"
       onmouseover="this.className='bcls2'" 
       onmouseout="this.className='bcls1'">
And we get:


We can even do transitions: on a click

Div3
With just a little bit of script:
   <script type="text/javascript">
var pntr=0;
var obj;
var x;
function shift()
{
   clearInterval(x);
   obj = document.getElementById('div3');
   pntr=0;
   obj.style.backgroundPosition='-116px -2px';
   x=setInterval('detailshift()',50);
}
function detailshift()
{
   pntr=pntr-2;
   obj.style.backgroundPosition=pntr+'px -2px';
   if (pntr<-116) 
   {
      clearInterval(x);
   }
}
   </script>
Even more options here.
Perhaps even a thumbnail viewer from a single image.


Now just one more little transitional effect that does not need images. Try clicking this and watch what happens to the box:


Think of the background as a layer with a z-index of negative infinity. It is always on the bottom. And you can apply all kinds of effects with a little scripting on the CSS.
It is just another example of scripting against the CSS. This time we just change the value for the background color over time:
There are 4 dimensions to web pages -- use them!

Cd&


post to Dzone Digg this! Add to del.icio.us Googleize this Add to reddit Save to myYahoo Add to furl Add to Netvouz! Spurl this! Add to Linkroll! Save to Simpy Give if thumbs up on StumbleUpon Save to Blinklist Add to Tektag Save to Bibsonomy Submit to Tweako
Search ERT on the Tools Page
Did you know? You can discuss this article with the mentor who wrote it and others interested in the topic? You are invited to join the discussion with Go to the forum

Got a technical article or tutorial you want to publish on the Internet? Join Go to the forum in the Round Table Forum and let the Mentors know what you have. If it meets ERT standards, is factual and can help ERT visitors, then ERT Mentors and Editors can help you (without charge) polish your offering so it can be published and promoted by ERT. An article published on ERT may be read by as many as 10,000 visitors a week; promoting you, your site, and your ideas. Please note ERT does not publish re-prints; promotional handouts, or pieces consisting mainly of links. So original technical content only please. If you prefer you can email the Editor