基于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程序设计有所帮助。
相关推荐
nmgxzm00 2020-11-10
ifconfig 2020-10-14
hhanbj 2020-11-17
zfszhangyuan 2020-11-16
古叶峰 2020-11-16
一个智障 2020-11-15
jipengx 2020-11-12
81427005 2020-11-11
xixixi 2020-11-11
游走的豚鼠君 2020-11-10
苗疆三刀的随手记 2020-11-10
Web卓不凡 2020-11-03
小飞侠V 2020-11-02
帕尼尼 2020-10-30
爱读书的旅行者 2020-10-26
帕尼尼 2020-10-23
杏仁技术站 2020-10-23
淼寒儿 2020-10-22