tags:

views:

306

answers:

4

I have a div that has a 'home.jpg' image as background and I want the background to be 'another_image.jpg' when I click something. The thing is that I want the new background to fadeIn and not just to appear (by doing some .css('background', url('another_image.jpg')); )

Is there a way to do that? I've tried the bgImageTransition plugin but it just adds another element on top of the div I had...

Thanks,

+3  A: 

The only way to do this is to fade in another element with the new background.

Alternatively, you could change the background immediately, then create a new element with the old background and fade it out.

SLaks
A: 

EDIT: added a cross-fade effect: http://jsfiddle.net/8vHa2/3/

(Click the JQuery logo)

Keep in mind that this kind of effects need a second element to be faded out or in, so my recommendation is for you to stick with the plugin you were trying.

Ben
He probably wants a cross-fade.
SLaks
@SLaks - Well, then he has no choice but to fadeOut the first elements over a second one like you suggested, I've edited my example with a cross-fade solution.
Ben
You can call `this.after()`.
SLaks
+1  A: 

You should check out this blog post about animating background images... it is a very cool effect. All you would have to do is turn your two images into a sprite and use that script.

Here is the demo.

fudgey
That's a neat trick, but it requires dedicated images.
SLaks
A: 

DEMO: http://jsbin.com/ewoje3/2

try this:

$('.img_div').fadeOut( 100 ,  function(){
$(this).css('background','url(my_new_img.jpg)').fadeIn(100);
});
aSeptik
He presumably wants a crossfade.
SLaks
Yeah, but I guess I won't use it anymore since I don't want to add another element. It's not that important for the application anyway :P
Hock