很簡單的一段背景顏色時刻發生變化的代碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> #dv {width: 100px;height: 100px;} </style> </head> <script> window.onload=function(){ var oDv = document.getElementById('dv'); oDv.style.backgroundColor='red' setInterval(function(){ var arrRandNumber = [ getRandomNumber(0,255), getRandomNumber(0,255), getRandomNumber(0,255) ]; oDv.style.backgroundColor='rgb('+arrRandNumber[0]+','+arrRandNumber[1]+','+arrRandNumber[2]+')' function getRandomNumber(rMin,rMax){ var cha = rMax-rMin; var rand = Math.random(); //return (rMin+Math.round(cha*rand)) } },500) //alert(getRandomNumber(0,255)) }; </script> <body> <div id="dv"></div> </body> </html>