Home  >  Article  >  Web Front-end  >  js获取随机颜色值的函数_javascript技巧

js获取随机颜色值的函数_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:21:401398browse

收集一个函数:获取随机颜色值

复制代码 代码如下:

function getRandomColor(){
return "#"+("00000"+((Math.random()*16777215+0.5)>>0).toString(16)).slice(-6);
}


说明:

1、16777215为16进制的颜色ffffff转成10进制的数字
2、>>数字取整
3、转成16进制不足6位的以0来补充

在线演示代码:


[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn