Home > Web Front-end > JS Tutorial > 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
Release: 2016-05-16 17:19:10
Original
1732 people have browsed it

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.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template