//隨機變換元素背景色
//Math.random() * (大 - 小) + 小
function color () {
var r = parseInt(Math.random() * 256);
var g = parseInt(Math.random() * 256);
var b = parseInt(Math.random() * 256);
return 'rgb(' + r + ',' + g + ',' + b + ')';
}
setInterval('changColor.style.background = color()',100);
//隨機變換元素內容,邊框色,和字色
<div id="changColor" style="width: 300px; height: 300px; border-radius: 50%; margin: 300px auto;font-size:30px;line-height:300px;text-align:center;border:3px #ccc solid;">愛你哦
</div>
<script type="text/javascript">
var names =['趙小倩', '張鉞涵', '張鉞瞾', '張鉞坤','張龍','火雲邪神'];
var colors = ['black','skyblue','blue','red'];
changColor.childNodes[0].nodeValue = names[0];
function changeCont () {
var i = parseInt(Math.random() * names.length);
changColor.childNodes[0].nodeValue = names[i];
// 來點顏色瞧瞧
let r = parseInt(Math.random() * 256);
let g = parseInt(Math.random() * 256);
let b = parseInt(Math.random() * 256);
return 'rgb(' + r + ',' + g + ',' + b + ')';
}
setInterval('changColor.style.color = changeCont()',200);
setInterval('changColor.style.borderColor = changeCont()',500);
</script>
複製程式碼