基於javascript實現隨機顏色變化效果,javascript實現
本文執行個體講解了基於javascript實現隨機顏色變化效果,分享給大家供大家參考,具體內容如下
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>隨機顏色變化效果</title> <style type="text/css">#thediv{ width:100px; height:100px;}</style><script type="text/javascript"> var colorList=["#FFFF99","#B5FF91","#94DBFF","#FFBAFF","#FFBD9D","#C7A3ED","#CC9898","#8AC007"]; for(var i=0;i<colorList.length;i++){ var bgColor=getColorByRandom(colorList); } function getColorByRandom(colorList){ var colorIndex=Math.floor(Math.random()*colorList.length); var color=colorList[colorIndex]; colorList.splice(colorIndex,1); return color; } window.onload=function(){ var odiv=document.getElementById("thediv"); function func(){ odiv.style.backgroundColor=getColorByRandom(colorList); } setInterval(func,1000);}</script> </head> <body> <div id="thediv"></div></body> </html>
另一個js函數實現隨機顏色:
function randomcolor(){var colorvalue=["0","2","3","4","5","6","7","8","9","a","b","c","d","e","f"],colorprefix="#",index;for(var i=0;i < 6; i++){index=Math.round(Math.random()*14);colorprefix+=colorvalue[index];}return colorprefix;}var test=randomcolor();alert(test);
以上就是本文的全部內容,希望對大家的學習javascript程式設計有所協助。
您可能感興趣的文章:
- 一個js隨機顏色指令碼(用於標籤頁面,也可用於任何頁面)
- js隨機顏色代碼的多種實現方式
- js擷取隨機顏色值的函數
- javascript產生隨機顏色範例程式碼
- js代碼實現隨機顏色的小方塊