Creating a Mouseover Fade Effect with jQuery
by c.bavota | Posted in Tutorials | 214 comments
My last little jQuery tutorial was an alternative to using CSS to create an image change on a mouseover. Now I want to take that one step further and add a fade effect. For my example, I am going to make a black and white image fade into a color image. To achieve this effect I am going to introduce the animate function. There are tons of possibilities in regards to jQuery’s animate function, but for this tutorial, I’m going to keep it simple by just using it to do one thing.
First things first, download jQuery. Grab the Production minified version off of their site. Next, get two images. I used the following.


Add the jQuery script between your head tags.
<script type='text/javascript' src='http://yoursite.com/jquery.js'></script>
Here is the function.
<script type='text/javascript'>
$(document).ready(function(){
$("img.a").hover(
function() {
$(this).stop().animate({"opacity": "0"}, "slow");
},
function() {
$(this).stop().animate({"opacity": "1"}, "slow");
});
});
</script>
Here is the CSS.
<style>
div.fadehover {
position: relative;
}
img.a {
position: absolute;
left: 0;
top: 0;
z-index: 10;
}
img.b {
position: absolute;
left: 0;
top: 0;
}
</style>
And here is the body code.
<div class="fadehover"> <img src="cbavota-bw.jpg" alt="" class="a" /> <img src="cbavota.jpg" alt="" class="b" /> </div>
All together you got something that looks like this.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>jQuery Hover Effect</title>
<script type='text/javascript' src='jquery.js'></script>
<script type='text/javascript'>
$(document).ready(function(){
$("img.a").hover(
function() {
$(this).stop().animate({"opacity": "0"}, "slow");
},
function() {
$(this).stop().animate({"opacity": "1"}, "slow");
});
});
</script>
<style>
div.fadehover {
position: relative;
}
img.a {
position: absolute;
left: 0;
top: 0;
z-index: 10;
}
img.b {
position: absolute;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div class="fadehover">
<img src="cbavota-bw.jpg" alt="" class="a" />
<img src="cbavota.jpg" alt="" class="b" />
</div>
</body>
</html>
Test it out below:


NOTE: If you are using jQuery with WordPress, you need to replace all the dollar signs ($) with the word jQuery due to other Javascript libraries that use the dollar sign.



Hey and thank you very much for this script, it is really awesome!
I have a problem and a question though, I’m building my new website and look what happens: http://www.avantgande.com/eyedr18/ I have tried to make this work with one image so far as a test (the girl with the dark hair with the read more tag) and while the fading part works great, the image is suddenly not at the right place anymore, and I really can’t tell what went wrong, my code looks clean..
Also, I have lost Lightbox functionality here, is that possible?
Thanks!
Mx
Okay, in some weird way I managed to fix the first problem.. Now how about Lightbox integration?
</div
this is how my code looks like now, but obviously no result..
<a href="images/image-1.jpg"> </a>Okay the code is not showing up nicely at all..
Paste your code between <pre> tags like indicated in the instructions below.
But I am not too familiar with Lightbox so I don’t know if I can help you out.
Can you do above example without absolute position tag?
The site elements get misplaced while switching between bigger and smaller screens.
You can’t do it without absolute positions because the elements need to sit on top of one another.
Hey I tried that but clearly it did not work.
But here is where I am now: http://www.avantgande.com/eyedr18/
The buttons underneath the first Flash banner are finally positioned in the right place, but at the very bottom of the screen is another line of thumbnail-buttons, that are now up too high (they should be on the black bar underneath).. But whenever I try to change a series of buttons, the other line also shifts because of that absolute tag I believe.. Anything I can do about this?
Mx
Ok I was able to solve my problem, just worked with more relative tags and now everything is fixed, except the Lightbox problem. Here goes my code again, trying with pre tags:
<a href="images/image-1.jpg"> </a>Damn it, same result and used the pre-tags. You can check it in my source code if you want to, it’s plain html.
Mx
It seems to be fine. I guess you figured it out.
Just to let you know, you can use this for your jQuery instead of what you have:
$("img.a, img.c, img.e").hover( function() { $(this).stop().animate({"opacity": "0"}, "slow"); }, function() { $(this).stop().animate({"opacity": "1"}, "slow"); });Cuts down on the code and works the same.
Thanks! I did figure it out.
Thanks for the tip on how to shorten it.
Do you have any idea on how to get the lightbox feature back up?
I don’t really use Lightbox so I am not too clear on what the problem might be.
hey.
you are full of helpful tips and tricks. alas i require additional help. after much fighting i finally got the fade to work. problem is that it only fades to the different image once and then leaves it there. as you can see:
http://www.blamethesky.com/site2.html
assistance would be super.
Sure you copied the code completely right? Changed the jquery?
This is just what I was looking for, and it’s really easy! Thank you for sharing!
But i’m stucked with a little problem: could it be that it doesn’t work inside a php include? I run it separately and works just fine, but when I separate everything to fit the modules it just won’t make the fade effect.
I checked and triple checked everything, and all seems to be correct.
Did this happen to anyone else?
Never mind, already fixed it. I had to call Jquery again, it was so stupid ¬_¬
Thank you for the amazing tutorial!
Excellent! I’ve been trying to work this out for ages! And the tutorial was so easy to follow. I will now look for your other posts!
I like this script very much!
I’m looking for a way to realize a gallery using previous – next – play – buttons. Each photo should start in B/W and fade to color using a timer value. Showing a text below the picture (without fading) would be great. Do you know any gallery script that can do that – with some small additional programmings ?
Do you mean there is a chance to work with only one color photo at a time ?
Hi there – great piece of code and really nice effect! I have been looking for something similar to this – is there a way that when you rollover the image, you can have 2 hotspots (ie 2 different links)? I have been wrecking my brain to try and find a solution!
Thanks!
Kerry
Hey, this is great.
I am trying to center it, though. I know that absolute positioning is req’d in order to make the images display on top of each other, but would it be possible to center-align the “fadehover” tag?
I am new to this stuff, so I’m sorry if this is newbie stuff, but I couldn’t find a sol’n online.
Thanks,
JOB
thank you! you are a credit to the web.
bit of an issue tho, the image fades out, but then doesnt fade back, any ideas? sorry i cant publish the page yet.
ta
mike
Make sure you have pasted the code exactly as it is above. If you have, then I don’t see why the fade in/out would not work properly.
Excelent Job mate, thank you!
Thanks for the nice jQuery tutorial! I wanted to create a menu button hover effect that was more interesting than the CSS hover and this is a great example of how easy it is to achieve that. Thanks again!
Thanks for the rollover. I’ll be using this in my next project. Cheers.
I’m using this code in 3 side-by-side “nav” divs. The page must also include prototype.js. (necessary to client) The script will run the first time I roll over an image, but fails to run after that. Any suggestions?
I want to use your great tutorial on my wordpress blog, i have a question, in which file do i put the script?
$(document).ready(function(){
$(“img.a”).hover(
function() {
$(this).stop().animate({“opacity”: “0″}, “slow”);
},
function() {
$(this).stop().animate({“opacity”: “1″}, “slow”);
});
});
Thanks a lot!
Cheers..
Usually it would be header.php
I have copied the code down and all seems to work fine, but when i add a link onto the page, the link does not work, if i remove one of the image rollovers the link works. Any ideas how to fix this?
Are you adding a link to the images or somewhere else on the page?
Thanks…I‚Äôll be using this in my next project
nice, but is there a simple solution for when i move the mouse more than once to the image and it keeps animating when i left the are with the cursor
hola!
Nice tutorial! is there a way to hide the images on body ready (load)?
$(document).ready(function(){ $(".a").hover( function() { $(this).stop().animate({"opacity": "1"}, "slow"); }, function() { $(this).stop().animate({"opacity": "0"}, "slow"); }); });Thanks so much for the great tutorial!
One note, in WordPress you don’t need to replace all the $ signs with the word jquery – you can just wrap the whole thing in this function:
http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_wrappers
Nice! Thanks for the straight forward tutorial, works like a charm. Is there an easy way to link the images?
Nice work, linking images work too! Love the positioning you created, very crafty. A must use in a project, thank u mucho!
i had idea to form menu by using this effect . but it is not supporting for multiple image on same page . it is working for only one set . what to do . help me