js从10种颜色中随机取色实现每次取出不同的颜色
作者:
昨天在做js 从10种颜色中随机取色,并每次取出的颜色不同,具体的实现思路如下,感兴趣的朋友可以参考下
昨天在做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;
}
这样便能每次取出的颜色是随机的且都不一样