Search code examples
jquerybackgroundfade

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


Solution

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