lor*_*afs 2 for-loop sass increment
我想做一些非常简单的事情,但似乎做不到,这让我发疯。我想判断我的数字是否为偶数,并将变量加一。这就是我一直在尝试的:
@for $i from 1 through 6 {
$rgba-opacity:0.5;
&:nth-child(#{$i}) {
background: rgba($color-white-primary, $rgba-opacity);
}
@if #{$i} % 2 == 0 {
$rgba-opacity: $rgba-opacity+0.5;
}
}
Run Code Online (Sandbox Code Playgroud)
基本上我想做的是一号和二号孩子获得一个颜色值,三号和四号孩子获得另一个颜色值,五号和六号孩子获得第三个颜色值。知道为什么这不起作用吗?非常感谢您的帮助。
在您的代码中(假设您已经粘贴了完整的代码),您&:nth-child()直接使用这将给出错误Base-level rules cannot contain the parent-selector-referencing character '&'.您需要为其指定一个父选择器。
$r : 100;
$g : 120;
$b : 140;
$rgba-opacity:0.5;
@for $i from 1 through 6 {
div {
&:nth-child(#{$i}) {
background: rgba($r, $g, $b, $rgba-opacity);
&:before {
content: ""+$i;
}
}
}
@if $i % 2 == 0 { //use $i for calculating mod here than #{$i}
$rgba-opacity: $rgba-opacity + 0.2;
$r: $r + 100;
$g: $g + 100;
}
}
Run Code Online (Sandbox Code Playgroud)
超文本标记语言
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
Run Code Online (Sandbox Code Playgroud)
输出:
