Quick & Easy image rollovers using jQuery

Tags: jQuery
Word Count: 1023

I have a navigation bar that I am working with that is made up of images. When you roll over these images we are doing a quick swap for the over image. The reason I am rewriting it is because it was boiler plate Dreamweaver code that uses the 10 year swap image functions and I wanted to do it by hand. Sure the old code works fine but to me its just not very clean and not easy for anyone to understand.

I am fully aware that I could of done a quick search and found a way better solution than this but nobody ever learned anything from cheating so I thought I would give it a try. I built out a fairly standard navigation that looks like this. Now that I have my navigation in place I need to react to the user mousing over the image. All of my images are in a directory called images and to keep it simple I have some easy naming conventions. For each navigation image I have one with _over suffix attached to it. For example, I have a home.png button and a home_over.png for the mouse over state.

It's magic time and I can't believe how easy this was. First we need to include our jquery library and to do that I am using Google's CDN so you can follow along at home. Next we need to loop over ever navigation item we have. To do that we use the following selector to get an array of items back and use the each method to loop over each. Finally for each loop we are going to get the link, the image and the image source. For each link we can add mouse over and mouse out events. In the mouse over method we are doing a simple replace. We are going to replace .jpg with _over.jpg and since the first part of the image name is the same this is all we have to do to swap images. Finally our mouse out just returns the image src to its original state.

As I said there is not much to it but its quite easy and works just fine for what I need it for. If you got some ways to improve this please feel free to chime in. As I said I am no expert and probably could of found something better but nobody ever learns that way. Now maybe I can take this and build a plugin because I have been meaning to write 1. Here is the complete code.

Comments

#1 Posted By: Adam Posted On: 8/18/09 2:47 PM
your function could be simplified using the jQuery hover event : http://docs.jquery.com/Events/hover

e.g. something like (untested but you get the idea)...

$(function() {
$("#navigation li").hover(
function () {$(this).attr(”src”, $(this).attr(”src”).replace(/.jpg/, “_over.jpg”));},
function () {$(this).attr(”src”, $(this).attr(”src”).replace(/-over.jpg/, “.jpg”));});
});
#2 Posted By: Adam Posted On: 8/18/09 2:48 PM
ooops, spotted I've used a '_over' on the first line and '-over' on the second, so it wont work as is :)
#3 Posted By: Claude Posted On: 8/19/09 12:52 AM
This is a good start, but you're still not dealing with the classic issue of pre-loading images and the newer one about performance.

Your solution will result in (1) flickering while the mouse over images are loaded for the first time; and (2) reduced performance since you're potentially downloading 10 images per page.

You can solve both problems by relying more on spriting and CSS background positioning. Take a look at http://www.alistapart.com/articles/sprites and http://www.alistapart.com/articles/sprites2/

Cheers.
#4 Posted By: Dan Vega Posted On: 8/20/09 6:07 PM |
Author Comment
@Adam - That is a lot quicker, thanks for the code!
@Claude - I was wondering what I could do about that, thanks for the info.
#5 Posted By: Orlando Website Design Posted On: 11/30/09 11:02 AM
I've been going back and forth on the best solution for my rollever menu. I'm all for code compliance and good backwards compatibility, so i'll probably go the CSS route. Thanks for the good info though!
#6 Posted By: Mark Posted On: 2/11/10 9:18 AM
#7 Posted By: deepak Posted On: 5/5/10 5:45 AM
this script is so good i like it and i use it my client portfolyo


Post Your Comment

Leave this field empty







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.