2014-08-27 35 views
-9

我需要在網站上做一個背景慢慢改變顏色,所以不斷從紅色,綠色,藍色,回到紅色。有沒有與jQuery或JavaScript的方式來做到這一點? 感謝緩慢變色背景

+1

是亞歷克斯,有。 – Alp 2014-08-27 12:22:32

+0

向下看這個頁面的右側,所有這些都是(非常)這個問題的愚蠢 – jbutler483 2014-08-27 12:23:40

+0

可能重複的[jquery背景顏色變化動畫不工作](http://stackoverflow.com/questions/16814767/jquery -background色變化 - 動畫 - 不工作) – 2014-09-03 10:54:54

回答

0
var color = "red"  
setInterval(function() { 
    if(color == "red") { 
     $('body').stop().animate({backgroundColor: "green"}, 2000); 
     color = "green"; 
    } else if(color == "green") { 
     $('body').stop().animate({backgroundColor: "blue"}, 2000); 
     color = "blue"; 
    } else if(color == "blue") { 
     $('body').stop().animate({backgroundColor: "red"}, 2000); 
     color = "red"; 
    } 
}, 2500); 
2

對於簡單的動畫,你可以使用CSS:

body { 
    background-color: red; 
    -webkit-animation: changeBgColor 3s infinite; 
    -moz-animation:  changeBgColor 3s infinite; 
    -ms-animation:  changeBgColor 3s infinite; 
    -o-animation:  changeBgColor 3s infinite; 
    animation:   changeBgColor 3s infinite; 
}  

@-webkit-keyframes changeBgColor { 
    0% { 
    background-color: red; 
    } 
    25% { 
    background-color: green; 
    } 
    50% { 
    background-color: blue; 
    } 
    75% { 
    background-color: black; 
    } 
    100% { 
    background-color: red; 
    } 
} 

@-moz-keyframes changeBgColor { 
    0% { 
    background-color: red; 
    } 
    25% { 
    background-color: green; 
    } 
    50% { 
    background-color: blue; 
    } 
    75% { 
    background-color: black; 
    } 
    100% { 
    background-color: red; 
    } 
} 

@-o-keyframes changeBgColor { 
    0% { 
    background-color: red; 
    } 
    25% { 
    background-color: green; 
    } 
    50% { 
    background-color: blue; 
    } 
    75% { 
    background-color: black; 
    } 
    100% { 
    background-color: red; 
    } 
} 

@-ms-keyframes changeBgColor { 
    0% { 
    background-color: red; 
    } 
    25% { 
    background-color: green; 
    } 
    50% { 
    background-color: blue; 
    } 
    75% { 
    background-color: black; 
    } 
    100% { 
    background-color: red; 
    } 
} 

keyframes changeBgColor { 
    0% { 
    background-color: red; 
    } 
    25% { 
    background-color: green; 
    } 
    50% { 
    background-color: blue; 
    } 
    75% { 
    background-color: black; 
    } 
    100% { 
    background-color: red; 
    } 
} 

JS Bin Example