js随机改变背景颜色

来源:互联网 发布:python socket 代理 编辑:程序博客网 时间:2024/05/02 00:37
这个程序里面,最让我感觉不错的是:
  1.   var r,g,b;  
  2.   r = decToHex(randomNumber(256)-1);  
  3.   g = decToHex(randomNumber(256)-1);  
  4.   b = decToHex(randomNumber(256)-1);  
  5.   document.bgColor = "#" + r + g + b; 

这段生成颜色的代码相当的赞,很不错的算法~~~

JavaScript代码

  1. function randomNumber(limit){  
  2.   return Math.floor(Math.random()*limit);  
  3. }function decToHex(dec)  
  4. {  
  5.   var hexStr = "0123456789ABCDEF";  
  6.   var low = dec % 16;  
  7.   var high = (dec - low)/16;  
  8.   hex = "" + hexStr.charAt(high) + hexStr.charAt(low);  
  9.   return hex;  
  10. }function randomBgColor()  
  11. {  
  12.   var r,g,b;  
  13.   r = decToHex(randomNumber(256)-1);  
  14.   g = decToHex(randomNumber(256)-1);  
  15.   b = decToHex(randomNumber(256)-1);  
  16.   document.bgColor = "#" + r + g + b;  
  17. }  
  18. randomBgColor(); 

 

 

http://www.footya.com/?action=show&id=12

原创粉丝点击