昨天在做js 從10種顏色中隨機(jī)取色,并每次取出的顏色不同的時(shí)候,考慮了很多,最終用如下來(lái)實(shí)現(xiàn):
代碼如下:
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;
}
這樣便能每次取出的顏色是隨機(jī)的且都不一樣
更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄