Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
72
rated 0 times [  73] [ 1]  / answers: 1 / hits: 42327  / 15 Years ago, tue, december 29, 2009, 12:00:00

How can I fade one image into another with jquery? As far as I can tell you would use fadeOut, change the source with attr() and then fadeIn again. But this doesn't seem to work in order. I don't want to use a plugin because I expect to add quite a few alterations.



Thanks.


More From » jquery

 Answers
30

In the simplest case, you'll need to use a callback on the call to fadeOut().



Assuming an image tag already on the page:



<img id=image src=http://sstatic.net/so/img/logo.png />


You pass a function as the callback argument to fadeOut() that resets the src attribute and then fades back using fadeIn():



$(#image).fadeOut(function() { 
$(this).load(function() { $(this).fadeIn(); });
$(this).attr(src, http://sstatic.net/su/img/logo.png);
});


For animations in jQuery, callbacks are executed after the animation completes. This gives you the ability to chain animations sequentially. Note the call to load(). This makes sure the image is loaded before fading back in (Thanks to Y. Shoham).



Here's a working example


[#97964] Thursday, December 24, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
khalilb

Total Points: 173
Total Questions: 110
Total Answers: 105

Location: Honduras
Member since Thu, Mar 23, 2023
1 Year ago
;