How to add fading or image transition effect using JQuery?

kheya picture kheya · Feb 1, 2011 · Viewed 27.1k times · Source

I have just one <img> element on my page. I change the src attribute of this image every 7 seconds.

I see the new images every 7 secs, but it would be nicer if I can add some fading or transitions effect when loading new image.

Does some have simple script for this?

I don't need any plugin. Just need some clue or few lines of sample for doing it.

Answer

Brad Christie picture Brad Christie · Feb 1, 2011

Despite what KaiQing mentioned, you can use the callbacks ability of jQuery to fade in/out the image while you're changing it. This can be done like so: http://www.jsfiddle.net/bradchristie/HsKpq/1/

$('img').fadeOut('slow',function(){
    $(this).attr('src','/path/to/new_image.png').fadeIn('slow');
});