10 09/2014

less variables use array

最后更新: Wed Sep 10 2014 12:29:11 GMT+0800

I want set random color to sth use lesscss.

@colorArr:'#00c,red,green';

.random_color(@arg){
    background-color:~`@{colorArr}.split(',')[@{arg}]`;
}

.random_color(){
    @len:`Math.floor(Math.random()*(@{colorArr}.split(',').length))`;
    background-color:~`@{colorArr}.split(',')[@{len}]`;
}

.div1 {
    height:300px;
    .random_color();
    .random_color(0);
}
  • use .random_color() to generate random color.
  • .random_color(N) get the N-index of colorArr.