Blog

November 16th, 2009
jquery

Fading Image Switch Jquery

I thought I would go really simple but effective in this jquery tutorial. Switching images using CSS is pretty simple by changing the background position, but that’s a straight switch, which is fine for most things but I wanted a smoother method which would fade the image from black and white to colour. This is not the most challenging of tasks but it’s worth doing, it is so easy to implement to multiple images.

Click here to view the demo

Right first thing is to make 2 images of the same size, one in colour and the other being black and white. Of course you don’t have to use black and white image, I am just using that as it clearly shows the fade transition. The image I am going to use was taken in Toronto on a tour of the Rogers Centre which is the home of the Toronto Blue jays MLB team.

Make sure you attach your jquery or none of this will work.

The Jquery

1
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

The HTML

1
2
<img src="colour.jpg" class="overlay_img"/>
<img src="bw.jpg" class="bw_img"/>

Nice and clear as always, real simple, just two images , add the class ‘overlay_img’ to the top image and ‘bw_img’ to the bottom. I thought these class names would make it easier to follow rather than image 1 and image 2.

The CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.overlay_img {
	position:absolute;/*this is really the only style you need*/	
	padding:10px;
	background-color:#333;
	border:1px solid #000;
	-webkit-box-shadow: 0px 0px 10px #FC0;  
	-moz-box-shadow: 0px 0px 10px #FC0;
}
.bw_img {
	padding:10px;
	background-color:#333;
	border:1px solid #000;
	-webkit-box-shadow: 0px 0px 5px #F60;
	-moz-box-shadow: 0px 0px 5px #F60;
}

As I have noted in the CSS you only really need absolute positioning on ‘overlay_img’ to make this effect work, I added in the rest to finish it off.

The Jquery

1
2
3
4
5
6
7
8
9
$(document).ready(function(){
	$('.overlay_img').fadeTo('fast', 0); //hides the image from view
	$('.overlay_img').hover(function(){ //when hovered...
		$(this).stop().fadeTo('normal', 1.0); //fade the overlay_img to 100% in normal speed
		}, 
		function(){   
		$(this).stop().fadeTo("normal", 0); //when not hovered fade back to 0% in normal speed
	});
});

This is all the jquery you need to produce this effect. If you have followed my blog posts on jquery you should understand most of this by now. Not really much of a write up in this one, all nice and straight forward.

Related Posts

Purely GPS is live!
At my Place of work Blue Solutions ltd. we have launched a side company c...
EA Sports Cover Design
If you didn't know I am a huge sports fan so I have been waiting for the ne...
Moving on...
After 31 months of working for Blue Solutions a leading software distribu...

4 Comments

  1. Avaxier
    April 15, 2010

    Great one! This is the simplest fade effect script I’ve ever encountered.

    Btw, how do you make the image loading fade in effect? Please mail me the answer if you have some spared time.. :)

    Thanks!

  2. admin
    April 15, 2010

    I take it you mean the outer glow. Its using the box shadow style in CSS.

  3. John
    May 15, 2011

    Firstly I just want to say, THANK YOU!
    Secondly, this is, by far, the most simple and straight forward tutorial I have come across with regards to this effect. I had spent the best part of at least 4 hours trying to follow other tutorials and for some reason could not figure them out.. Either I was an absolute moron, or, your tutorial had something theirs didn’t.

    Thanks again!
    -
    John

  4. Kevin Z.
    December 1, 2011

    Hey,

    Great tut! I am not the best at JQuery but because of people like you I am getting better. Recently, I used this code and when I went update the size of the images I wanted to use the code no longer worked any ideas?

    Thanks,
    Kevin Z.

Leave a comment