我正在研究有5种颜色列表的东西,我需要在10种不同的div
元素中使用这5种颜色。但是每种颜色都需要两次和随机顺序使用。使用random()
函数并不能真正起作用,因为我需要数字不重复。
我现在拥有的是:
$colorList: #32ba81,#65e028,#4791e0,#f44130,#ff7811;
@mixin getColor($colorCont) {
background-color: nth($colorList, $colorCont);
}
$j: 0;
@for $i from 1 to 6 {
@if ($i < 6) {
$j: $j + 1;
}
.card:nth-of-type(#{random(10)}) {
@include getColor($j);
}
.card:nth-of-type(#{random(10)}) {
@include getColor($j);
}
}
如何在不重复的情况下为nth-of-type
生成10个随机数?
注意:我有2个 .card:nth-of-type
同时使用两种div
元素使用一种颜色。
有标准方法可以生成非重复的随机数。对于一组,您将阵列洗牌:
-
将数字[0,0,1,1,2,2,3,3,4,4]放入阵列中。
-
洗牌。如果您没有内置的阵列混音,请使用Fisher-Yates shuffle。
-
从洗牌数组中挑选数字,使用它们为颜色索引。