您的位置:首页 > 其它

随机获取一种颜色值的三种方法

2016-11-16 10:12 309 查看

闲来无事,想起了初中时流行的山寨手机的跑马灯。于是想动手自己做一个。

那会的跑马灯都是红蓝两色居多,俗。

我这种二十一世纪的潮流人士肯定得做那种千百种颜色的。

姹紫嫣红,美哉。

这样做的话,颜色值就不能一个一个手写了,于是想做成不确定的颜色。

 

第一种写法:

function colorRandom() {
var a, b, c;
var a = parseInt(255 - Math.random() * 255).toString(16);
var b = parseInt(255 - Math.random() * 255).toString(16);
var c = parseInt(255 - Math.random() * 255).toString(16);
colorStr = '#' + a + b + c;

//alert(str)
};

  

 

第二种写法:

function colorRandom() {

colorStr = "#"+("00000"+((Math.random()*16777215+0.5)>>0).toString(16)).slice(-6);

//alert(str)
};

 

 

第三种写法:

function colorRandom() {

colorStr = "#"+("00000"+(Math.random()*0x1000000<<0).toString(16)).slice(-6);
//alert(str)
};

 

 

对了,外边记得声明一个colorStr把颜色值存起来。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: