昨天在做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;
}
這樣便能每次取出的顏色是隨機的且都不一樣