How to fade changing background image

fadejquery picture fadejquery · Feb 15, 2011 · Viewed 131.6k times · Source

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

Answer

MacMac picture MacMac · Feb 15, 2011

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);