27
Sep 2007
Easy, Reusable Image Rollovers with jQuery
Posted in Software, Web Dev by Atlanta Jones at 10:55 pm |

Okay, so this is my first jQuery tutorial (or any tutorial for that matter). So here goes…

In my own experience, image rollovers for navigation is a pretty repetitive task. Almost every site I build has them. For the longest time, I simply used the javascript spit out by Dreamweaver. Sure it worked, but it required including an extra javascript file, and inline javascript for mouseover and mouseout events.

The more I got into jQuery, the more I realized this was an ugly solution. And as I stopped using Dreamweaver for regular development over a year ago, I thought I’d come up with a solution that could just be easily dropped into each new site and would just work.

Before I start, I should note that usage of this script assumes a couple things. It assumes your navigation images are contained within a div with an id (I use #nav). It also assumes that your over state images are named consistently. For example, if your contact image is nav_contact.gif, your rollover image will need to be something like nav_contact_over.gif.

It also assumes your navigation is structured thusly:

<div><a><img></a><a><img></a></div>

First, make sure your jQuery code is surrounded by the usual:

1
2
$(document).ready(function() {
});

Now set up the mouseover event:

1
2
$("#nav a").mouseover(function(){
});

Next, set the source (src) of the image file. This is done by finding the child of the link that was moused over, in this case, an image.

imgsrc = $(this).children("img").attr("src");

We’ll only perform the rollover if the image state is not already “ON”.

matches = imgsrc.match(/_over/);

If it’s not, add the ‘_over’ bit to the filename and change the image source with attr(”src”):

1
2
3
4
if (!matches) {
	imgsrcON = imgsrc.replace(/.gif$/ig,"_over.gif");
	$(this).children("img").attr("src", imgsrcON);
}

And that’s it! Handling the mouseout is pretty simple. All you need to do is reset the image source to the value that was initially set on the mouseover:

1
2
3
$("#nav a").mouseout(function(){
	$(this).children("img").attr("src", imgsrc);
});

One more thing. If you have a lot of nav images, or they are not tiny files, you might want to preload them. Here is a function that will automatically preload all the images within the #nav div:

1
2
3
4
5
$("#nav img").each(function() {
	rollsrc = $(this).attr("src");
	rollON = rollsrc.replace(/.gif$/ig,"_over.gif");
	$("<img>").attr("src", rollON);
});

Line 1 creates a loop to run the following function for every image within the #nav div. Line 2 sets the image’s filename, and line 3 replaces ‘.gif’ with ‘_over.gif’. You can replace .gif with .jpg or whatever. And line 4 creates a new image element with the ON state of the image.

You can see the script in action here.
Download the .js file here.

So that’s it. This stuff is probably beyond basic for experienced jQuery users, but if you’re just getting started, hopefully this will help. I’ve got several other jQuery scripts like this, so hopefully this won’t be my last tutorial (even though this was more involved than I thought it was going to be :)


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

11 Responses:

Phunky said:

Although im sure you can tag more effects to the roll overs thanks to JQuery the demo as it is, is just totally over the top for a simple image roll over link.

Just because you can do this in JQuery does not mean you should!

I think you demo should take some advantage of the features that JQuery offers instead of just repeating something you can do a lot simpler in CSS


Atlanta Jones said:

I realize this is a common argument, and I fully agree it’s a viable direction. However I see absolutely nothing wrong with using images in navigation, especially in top-level section nav.

That said, define “simple” as it relates to accomplishing this in css. As a good example, just today, one of our contractors gave me a css file where navigation was done with css. It was accomplished my switching backgrounds on a tags on hover, etc. Just the css to do this navigation was 66 lines of css! This was for just five buttons. Plus, my method alleviates all the extra css mojo that might be necessary to make sure everything works in all browsers, etc.

My solution can be compressed into very few lines of code in comparison. And in the event someone has javascript disabled, it doesn’t exactly break the whole thing. The rollovers just won’t work. Small price to pay for the relatively tiny proportion of all visitors.

So yeah, point totally well taken, but I think this is still a good solution and might still help out some users just getting started with learning jQuery.


miko said:

this is great, thanks! works perfectly


Josh Byers said:

Thanks for you script. It is great if you just want a simple image rollover and much easier to implement than with CSS.

I’m using it on a test site right now http://project.familyperformancemarine.com


Image rollover on primary links Drupal howto « chirale said:

[...] Easy, Reusable Image Rollovers with jQuery [...]


HTML resource plea for help - WebProWorld said:

[...] plea for help Try this link: CSS rollover buttons or for something a little more complex: Atlanta Jones » Blog Archive » Easy, Reusable Image Rollovers with jQuery on a side note, for some reason perhaps a javascript, my firefox never seems to stop loading your [...]


How to make rollovers with jQuery « Category Icons said:

[...] 24, 2008 by submarine I’ve found a tutorial on Atlanta Jones. Read it, it’s worth it. Here is an example that works with the Default theme of WordPress [...]


greg said:

It seems everybody in the world is using jQuery these days and I’ve just downloaded it and begun to work with it. Your script is a great starting point. Thanks for posting this.


How to make rollovers with jQuery | Category Icons said:

[...] found a tutorial on Atlanta Jones. Read it, it’s worth it. Here is an example that works with the Default theme of WordPress [...]


Anthony Shaw said:

Atlanta… great work bro! Keep up the good work. Implemented on my site in just seconds.


I-_-I said:

Thank you for your script. Simple and well explained !!


Leave a Reply