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;
}


这样便能每次取出的颜色是随机的且都不一样


参考:http://www.verydemo.com/demo_c98_i18979.html

你可能感兴趣的:(js,随机,取数)