昨天在做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;
}
这样便能每次取出的颜色是随机的且都不一样
时间: 2024-09-14 21:40:26