JScript中的隨機(jī)顏色
一,其實(shí)隨機(jī)色就是RGB分別取三個(gè)0-256的隨機(jī)數(shù),
然后把你想要改的對(duì)象的樣式設(shè)置成這種顏色就行了。
var
?rndColor?
=
?
"
rgb(
"
?
+
?Math.round(Math.random()
*
256
)?
+
?
"
,
"
?
+
?Math.round(Math.random()
*
256
)?
+
?
"
,
"
?
+
?Math.round(Math.random()
*
256
)?
+
?
"
)
"
;
document.getElementById( " myData " ).style.color? = ?rndColor;
document.getElementById( " myData " ).style.color? = ?rndColor;
二,利用javascript中的setInterval()函數(shù),定時(shí)給某一個(gè)控件(如:span)的更換字體顏色樣式。
在javascript中,element.style.color屬性允許使用rgb(x,y,z)函數(shù),這樣我們就可以生成三個(gè)隨機(jī)數(shù)填充,以達(dá)到隨機(jī) ??? 變顏色的效果。我們可以用Math.random()來(lái)獲得隨機(jī)數(shù),然后再利用Math.round()來(lái)得到一個(gè)小于當(dāng)前值的最大整數(shù)。
RGB的值的隨機(jī)范圍是0-255,所以每個(gè)隨機(jī)值是:Math.round(Math.random()*256)。?
<span id="myData">Hello world</span>
<script language="javascript">
window.onload = function(){
?setInterval( rndMyData, 1000 );
}
function rndMyData(){
?var rndColor = "rgb(" + Math.round(Math.random()*256) +? "," + Math.round(Math.random()*256) + "," + Math.round(Math.random()*256) + ")";
?document.getElementById( "myData" ).style.color = rndColor;
}
</script>
<script language="javascript">
window.onload = function(){
?setInterval( rndMyData, 1000 );
}
function rndMyData(){
?var rndColor = "rgb(" + Math.round(Math.random()*256) +? "," + Math.round(Math.random()*256) + "," + Math.round(Math.random()*256) + ")";
?document.getElementById( "myData" ).style.color = rndColor;
}
</script>
???
posted on 2007-03-23 10:50 鴻雁 閱讀(412) 評(píng)論(0) 編輯 收藏