昨天在做js 从10种颜色中随机取色,并每次取出的颜色不同的时候,考虑了很多,最终用如下来实现:
复制代码 代码如下:
var colorList = ["#FFFF99","#B5FF91","#94DBFF","#FFBAFF","#FFBD9D","#C7A3ED","#CC9898","#8AC007","#CCC007","#FFAD5C"];
for(var i=0;i<lineList.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;
}
这样便能每次取出的颜色是随机的且都不一样
复制代码 代码如下:
var colorList = ["#FFFF99","#B5FF91","#94DBFF","#FFBAFF","#FFBD9D","#C7A3ED","#CC9898","#8AC007","#CCC007","#FFAD5C"];
for(var i=0;i<lineList.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;
}
这样便能每次取出的颜色是随机的且都不一样
标签:
随机取色,不同颜色
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“js从10种颜色中随机取色实现每次取出不同的颜色”评论...