在Less中并没有对数组遍历的原生支持,我们可以通过mixin递归的方式来实现遍历。
1、定义数组元素
1 | @colors: red, green, blue, black; |
2、获取数组元素长度
1 | @length: length(@color); |
3、数组元素取值
1 | @item: extract(@colors, @index); |
4、遍历
1 | @colors: red, green, blue, black; |
以上的less会被编译成以下css:1
2
3
4
5
6
7
8
9
10
11
12div {
background: red;
}
div {
background: green;
}
div {
background: blue;
}
div {
background: black;
}

