2013-08-17 108 views
0

我真的沒有關於JS的線索,但需要對我的隨機身體背景圖像有一個淡入效果。任何人都可以請我指出正確的方向。淡入淡出身體背景

<script type="text/javascript"> 
var totalCount = 5; 
function ChangeIt() 
{ 
var num = Math.ceil(Math.random() * totalCount); 
document.body.background = 'images/'+num+'.jpg'; 
document.body.style.backgroundSize = "cover"; 
document.body.style.backgroundPosition = "center"; 
} 
</script> 

謝謝

回答

0
<!DOCTYPE html> 
<html> 
<header> 
<script> 
$(document).ready(function(){ 
    $("#image").hide(); 
    $("#clickIt").click(function(){ 
     function ChangeIt(){ 
      var totalCount = 5; 
      var num = Math.ceil(Math.random(2) * totalCount); 
      return num; 
     } 
     var speed = ChangeIt(); 
     $("#Body img").fadeIn(speed, linear); 

    }); 
}); 
</script> 
</header> 
<body id="Body"> 
    <img id="#image" src="../apple.png" alt="img"> 
    <button id="clickIt"/> go </button> 
</body> 
</html> 

只是爲了你指出正確的方向,你可以使用jQuery做淡入淡出和。 上面是什麼,可能看起來像我敢肯定,這將是您的需求不同,但檢查出的文檔,研究了幾個令人沮喪小時後淡入在jQuery的網站jquery fadein api

1

一個例子,它最終歸結爲如下:
<body>不能有一個透明的背景圖像,因此它不能淡入。我的問題的解決方案是僞造一個白色的全屏div。我添加了一個簡單的CSS動畫,從1.0到0.0不透明度。