2010-03-25 68 views
0

我寫這個劇本旋轉背景圖像,每3秒,但它並沒有在所有的工作和我難倒,爲什麼不。jQuery的背景圖像旋轉腳本不起作用

$(document).ready(function() { 

     var inc = 0; 
     var bgImages = new Array(); 
     bgImages.push("Images/backgroundDog-small.jpg"); 
     bgImages.push("Images/backgroundDog1-small.jpg"); 
     bgImages.push("Images/backgroundDog2-small.jpg"); 
     bgImages.push("Images/backgroundDog3-small.jpg"); 

     setInterval(change, 3000); 

     function change() { 
      //if we're not at the end of the array 
      if (inc < (bgImages.length)) { 
       var image = bgImages[inc]; 
       $('body').css('backgroundImage', image); 
       console.log(image); 
       inc++; 


      //reset the counter inc and go through the array again 
      } else { 
       inc = 0; 
      } 
     } 
    }); 
+0

會發生什麼?圖像路徑是否正確? – SLaks 2010-03-25 02:13:49

+0

的背景是白色的。是的,圖像路徑是正確的。 – Catfish 2010-03-25 02:16:32

回答

5

CSS中的background-image屬性不會想到剛剛圖像URL;你需要像你在樣式表中一樣寫下它:url("example.png")

$('body').css('backgroundImage', 'url("'+image+'")'); 
+0

的偉大工程。謝謝 – Catfish 2010-03-25 02:19:26