Home  >  Article  >  Web Front-end  >  js randomly selects colors from 10 colors to achieve different colors each time_javascript skills

js randomly selects colors from 10 colors to achieve different colors each time_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:19:101708browse

Yesterday I was doing js to randomly select colors from 10 colors, and every time I took out a different color, I thought a lot, and finally implemented it as follows:

Copy code The code is as follows:

var colorList = ["#FFFF99","#B5FF91","#94DBFF","#FFBAFF","#FFBD9D", "#C7A3ED","#CC9898","#8AC007","#CCC007","#FFAD5C"];
for(var i=0;ivar bgColor = getColorByRandom(colorList);
}
function getColorByRandom(colorList){
var colorIndex = Math.floor(Math.random()*colorList.length);
var color = colorList[colorIndex] ;
colorList.splice(colorIndex,1);
return color;
}

In this way, the color taken out every time is random and different.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn