@for可以按如下方式反转 SCSS 中迭代输出的顺序:
$colors : green, gold, red;
a{
$i : length($colors);
@each $c in $colors {
&:nth-child(#{$i}){
fill:$c;
}
$i : $i - 1;
}
}
Run Code Online (Sandbox Code Playgroud)
a:nth-child(3) {
fill: green;
}
a:nth-child(2) {
fill: gold;
}
a:nth-child(1) {
fill: red;
}
Run Code Online (Sandbox Code Playgroud)
有谁知道有一种更“SCSS 原生”的方式来实现这一目标,从而提高可读性?
你的解决方案没问题。
您可以创建一个函数来反转数组:
@function reverse($list, $recursive: false) {
$result: ();
@for $i from length($list)*-1 through -1 {
@if type-of(nth($list, abs($i))) == list and $recursive {
$result: append($result, reverse(nth($list, abs($i)), $recursive));
}
@else {
$result: append($result, nth($list, abs($i)));
}
}
@return $result;
}
$list: #aaa, #bbb, #ccc, #ddd;
$new-list: reverse($list);
@for $i from 1 through length($new-list){
div:nth-child(#{$i}){
color: nth($new-list, $i);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用nth-last-child选择器。
@for $i from 1 through length($list){
div:nth-last-child(#{$i}){
color: nth($list, $i);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以重写数组:$list: #ddd, #aaa, #bbb, #ccc并使用nth-child.
| 归档时间: |
|
| 查看次数: |
4625 次 |
| 最近记录: |