RolloverImage rollover is the classic effect that we all have been playing for ages. And we often use it to rollover image tabs or menus.

Although I personally prefer to use text for navigation, but at times, when wearing the designer’s hat, I just can’t resist the temptation to make the design looks perfect through images.

Image rollover started with the traditional lengthy Javascript codes that preload the rollover images, and and setting the onmouseover and onmouseout events for each of the link involved.

<script text="type/javascript">
  // some javascript codes to preload images, 
  // rollover and rollback images
</script>

<a href="#" onmouseover="rolloverImage('about_us')" 
    onmouseout="rollbackImage('about_us')">
  <img src="images/about_us.gif" width="100" height="25" 
    alt="About Us" border="0" />
</a>

Many hated to write the Javascript rollover codes, because the codes are lengthy and ugly. So, people found a new way to create the rollover effect through Cascading Style Sheet (CSS) or Flash. I’m not going to talk about flash over here. So, no Javascript involved, great!

But, writing the CSS rollover ain’t a simple task too. Instead of writing Javascript, now you have to write the CSS styles, and the normal state and the hover state.

a.about_us {
  display: block;
  width: 100px;
  height: 50px; 
  background: url(images/about_us.gif) 0 0 no-repeat;
}
a.about_us:hover, a.about_us:active {
  background: url(images/about_us_over.gif) 0 0 no-repeat;
}

If you have 10 links, rest assured, you will need to write a pretty long CSS. And if you need display a different image for the currently selected link.

There is also another disadvantage of using CSS Image rollover, the famous flicker effect on IE 6. So on some occasions, you may want to just use a simple unobtrusive image rollover javascript.

With Rollover, you just have to bother about creating your images, the normal state and the hover state, write your HTML codes like usual without the mouseover or mouseout event, and unobtrusively add the rollover effect through a simple javascript call. Rollover also automatically preloads the rollover images on document load.

<ul id="nav">
  <li><a href="images/about_us.gif" width="100" height="50"
       alt="About Us"></a></li>
  ....
</ul>

<script type="text/javascript">
  window.onload = function() {
    new Rollover('nav');
  }
</script>

// or add you can add it to body onload event
<body onload="new Rollover('nav');">

No CSS involved, and with just a simple javascript call, you are done.

Demo and Download

15 Comments . Comments Feed . Trackback URI
Tue, 22 May 07 04:01 pm . Miccheng wrote:

Looks like a really elegant way of doing rollovers for JavaScript / CSS newbies. Good job!

Neat idea using the convention over configuration method for naming files. Good idea preloading the images too - dun like the flash when loading new images.

Personally, i prefer text based navs with background… don’t like to have to create a multiplicity of roll-over images.

So what if i don’t create a _sel image. Does it default to the _over image?

Tue, 22 May 07 06:19 pm . Herryanto Siatono wrote:

Thanks michael. I too, personally would prefer to use text based navs. But at times, coming from designer perspective, we tend to be perfectionist. :)

The ‘_sel’ option is to help the script to remove it from the image name if you are using a selected image, so it knows how to retrieve the ‘_over’ image.

If you don’t have selected image. You don’t have to bother about it at all.

Sat, 28 Jul 07 07:36 pm . yohann wrote:

Hi Herryanto

I cannot find the page anymore for the rollover tutorial, only google cahing and the dowload link is no more available. Could you let me know if that’s normal ?

I looked on the web for a rollover solution juste like yours and there ain’t so many.. I think it’s very nicely done and want to thank you for sharing.

Sun, 29 Jul 07 06:45 pm . Herryanto Siatono wrote:

Sorry about it Yohann, I just moved my web hosting, and forgot to upload some files. It’s up now.

Sun, 29 Jul 07 08:17 pm . yohann wrote:

Hi Herryanto,

Thank you for the fast fix, it’s very nice.

You’re library is a bless for those who don’t like to write tone of css code or ugly javascript everywhere in between html tags.

From my experience, when you apply the observer to the closest div, it even works faster than inline javascript and as fast as classic css over. you code is clean, small and fast !

I’ve tried as well to put the observers on all chunks of pages (content, lateral bar) while still in development and it works as expected, just with a bit more latency, but acceptable.

I’m using rails and the image caching mechanism doesn’t prevent over images to be correctly loaded, it was a nice surprise as well.

Well Herryanto, thanks again for that creative and very usefull piece of javascript.

Mon, 30 Jul 07 10:17 am . Herryanto Siatono wrote:

Glad that you find it useful Yohann. :)

Wed, 5 Sep 07 03:31 am . Jean Carlo wrote:

Hi,

I am trying use it with the prototype 1.5.0_rc0 library because i am using rails 1.1.6, and i have it error

this.links[i].observe is not a function
[Break on this error] this.links[i].observe(’mouseover’, this.rolloverImage);

Thanks for you help

Thu, 29 Nov 07 01:41 pm . S Woodside wrote:

You should probably note somewhere here or in the docs that prototype 1.5.1 is required, in case people are trying to use it with an older version they will get “$$ is not defined” (which doesn’t search in google).

Sun, 30 Mar 08 04:24 am . yosax wrote:

How to roll over image such as in Amazon?
I want to implement the codes on one of my web.

Mon, 2 Jun 08 11:16 pm . Nathaniel Bibler wrote:

I’d like to suggest an update to the rollover.js (v1.1) file, line 36:

images = $$(’#’ + this.id + ‘ a img’);

Instead of:

images = $$(’#’ + this.id + ‘ img’);

The main issue being that images that are not contained in an anchor tag should not be a rollover image. Further, navigation links may have separator images or other supporting graphics strewn between them. In the current implementation, the script attempts to load the ‘_on’ version of all images within the given DOM ID, which raises HTTP 404 errors for non-rollover-intended images (separator graphics and such).

Tue, 3 Jun 08 10:07 am . Herryanto Siatono wrote:

@Nathaniel, yeah it makes sense, will put a patch for that. Thanks.

Tue, 10 Jun 08 06:25 am . James Mason wrote:

It seems the download has gone missing again. Can you please make this available for download?

Tue, 10 Jun 08 12:13 pm . Herryanto Siatono wrote:

@James Sorry about it, the link is up now.

Thu, 4 Sep 08 06:34 am . Francesco Mazzi wrote:

Great work!! I’m using it on a website I’m developing.
I’m trying to add a new function to the class, so that when a link is clicked the image becomes automatically in the “_sel” state…useful if the menĂ¹ opens other submenus without reloading the page. Do you have any kind of suggestions? I’ll put there my results…thanks…bye bye

Sat, 4 Oct 08 12:38 pm . CodeSense wrote:

I still sometimes had the problem of the preloading not working in all browsers, I came up with a little bit better solution that extends the class you have here. Basically it creates a new image object for both the original and rolled over images and saves those as private members of the class. When you roll over or out of the image, it swaps in or out those image elements.

You can check it out here: http://you-dev.com/tutorials/articles/Automatic+preloading+Prototype+based+rollovers

Add Your Comment



(optional)