2014-03-30 127 views
0

我有以下代碼:添加背景的淡入淡出效果改變使用jQuery

$(document).ready(function() { 

    var bgCounter = 0, 
     backgrounds = [ 
      "images/2.png", 
      "images/1.png", 
      "images/3.png" 
     ]; 

    function changeBackground() 
    { 
     bgCounter = (bgCounter+1) % backgrounds.length; 
     $('#page').css('background', '#000 url('+backgrounds[bgCounter]+') no-repeat'); 
     setTimeout(changeBackground,100000); 
    } 

    changeBackground(); 
    }); 

我需要添加/淡出效果淡入,背景變化之間。任何指導?

回答

1

只需補充一點:

$('#page').fadeIn()

$('#page').css('background', '#000 url('+backgrounds[bgCounter]+') no-repeat');

這:$('#page').fadeOut()

吧..

+0

謝謝,這個工作:) – uniqezor

+0

你是最歡迎:) –

2

試試這個:

$(document).ready(function() { 
var bgCounter = 0, 
    backgrounds = [ 
     "images/2.png", 
     "images/1.png", 
     "images/3.png" 
    ]; 

function changeBackground() 
{ 
    bgCounter = (bgCounter+1) % backgrounds.length; 
     $('#page').animate({ 
    opacity: 0 
    }, 0).css({ 
      'background-image': 'url('+backgrounds[bgCounter]+')' 
    }).animate({opacity: 1}, 1000); 

setTimeout(changeBackground,10000); 
} 

changeBackground(); 
}); 

演示:http://jsfiddle.net/nq7uE/8/

+0

謝謝M8,它的工作原理,但卡里姆AG例如,對於我工作得更好 – uniqezor