Aug
05
2009

Creating a Mouseover Fade Effect with jQuery

by   |  Posted in Tutorials  |  200 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.

cbavota-bwcbavota


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.

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
Share the love...

Tags: , , , , , , , , , , , , , , , , , , ,

Short URL: http://bit.ly/bETRnX

Discussion 200 Comments

  1. Adam on March 6, 2010 at 1:59 pm

    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!

  2. Rainer on March 6, 2010 at 4:52 pm

    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 ?

  3. Kwoskillen on April 1, 2010 at 7:27 am

    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

  4. Jorvus O'Booch on April 4, 2010 at 8:01 pm

    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

  5. mike on April 21, 2010 at 9:06 am

    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

    • c.bavota on April 21, 2010 at 9:45 am

      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.

  6. Pepe on April 28, 2010 at 2:10 pm

    Excelent Job mate, thank you!

  7. acepek on May 3, 2010 at 9:19 pm

    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!

  8. Kathryn on May 5, 2010 at 4:19 pm

    Thanks for the rollover. I’ll be using this in my next project. Cheers.

  9. Jeremiah on May 7, 2010 at 3:57 pm

    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?

  10. Jorge on May 8, 2010 at 1:03 pm

    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..

  11. Majka on May 11, 2010 at 4:40 am

    Usually it would be header.php

  12. kevin on June 4, 2010 at 9:35 am

    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?

    • c.bavota on June 4, 2010 at 11:28 am

      Are you adding a link to the images or somewhere else on the page?

  13. HTML Artists on June 14, 2010 at 8:40 am

    Thanks…I‚Äôll be using this in my next project

  14. tomika on June 20, 2010 at 12:45 pm

    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!

  15. Ro on June 20, 2010 at 6:03 pm

    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");  
    		});  
    	});
    <a href="#" title="Test Title" / rel="nofollow"></a><!-- end item -->
  16. Michelle on June 20, 2010 at 6:22 pm

    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

  17. r.mcrae on June 22, 2010 at 2:05 pm

    Nice! Thanks for the straight forward tutorial, works like a charm. Is there an easy way to link the images?

  18. r.mcrae on June 24, 2010 at 12:17 pm

    Nice work, linking images work too! Love the positioning you created, very crafty. A must use in a project, thank u mucho!

  19. eonsofttech on July 12, 2010 at 2:16 am

    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

  20. StuartH on July 17, 2010 at 1:44 pm

    Excellent – thanks very much for this tidy way of doing this.

  21. Lauren on July 26, 2010 at 4:49 pm

    This is great! Thanks!

    I’m having trouble applying this to multiple objects though. Can I use the same function, classes etc on another div to have this same effect on another image?

    • c.bavota on July 27, 2010 at 9:07 am

      Using the same code multiple times should work for you. But you must use the same div container name, or make sure that the new div container has position: relative; set.

  22. lukeMV on July 28, 2010 at 2:12 pm

    I am trying to have several images next to eachother, but for some reason they keep stacking on top of each other. I’ve tried adding divs to clear the space between but without luck. What is the best way to have it so the images lay in a row next to eachother?

  23. Unicorn on August 3, 2010 at 10:54 am

    Hi Guys, am trying to do the same effect for background image, i have a div in its class it have a bg image, when mouse over am changing the class of the div (class with the glow bg image), however am not able to see this animated thing… it just changes like no smoothness… i tried fade, animate couldn’t success, any idea guyz?

  24. Zach on August 3, 2010 at 12:10 pm

    Hey just thought I’d let you know I am using (and greatly appreciate) this code. I did however tweak it.
    Semantically I did not really like having two img tags for one image, so I put a background on the one img’s containing element.
    I think this slightly leaner and cleaner:

    --HTML--
     
     
     
     
    --CSS--
    #img {
      position: relative;
      background:url(img.png);
      width:260px;
      height:189px;
      }
    .off {
       position: absolute;
       left: 0;top: 0;
       }
     
    --JQuery-- 
    $(".off").hover(....
  25. Zach on August 3, 2010 at 12:13 pm

    hmmm I guesse that pre – /pre code block doesn’t allow HTML. Oh well thats what an imagination is for.