Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fade changing background image

I want to fade the images when I do this code:

$("#large-img").css('background-image', 'url('+$img+')'); 

I've tried putting fade in so many places.

Thanks

like image 631
fadejquery Avatar asked Feb 15 '11 10:02

fadejquery


People also ask

How do I fade one side of an image in Photoshop?

In the Layer menu, select Layer Mask, then Reveal all. Use the Gradient tool to draw a line from inside the image toward the side or corner you want to fade away. Play around with this until the appropriate effect is achieved.


2 Answers

This is probably what you wanted:

$('#elem').fadeTo('slow', 0.3, function() {     $(this).css('background-image', 'url(' + $img + ')'); }).fadeTo('slow', 1); 

With a 1 second delay:

$('#elem').fadeTo('slow', 0.3, function() {     $(this).css('background-image', 'url(' + $img + ')'); }).delay(1000).fadeTo('slow', 1); 
like image 56
MacMac Avatar answered Oct 10 '22 19:10

MacMac


Can I offer an alternative solution?

I had this same issue, and fade didn't work because it faded the entire element, not just the background. In my case the element was body, so I only wanted to fade the background.

An elegant way to tackle this is to class the element and use CSS3 transition for the background.

transition: background 0.5s linear;

When you change the background, either with toggleClass or with your code, $("#large-img").css('background-image', 'url('+$img+')'); it will fade as defined by the class.

like image 29
Rampant Creative Group Avatar answered Oct 10 '22 18:10

Rampant Creative Group