For loop 如果for循环中的偶数为sass

For loop 如果for循环中的偶数为sass,for-loop,sass,increment,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-

我试图做一些非常简单的事情,但似乎做不到,这让我发疯。我想知道我的数字是否为偶数,并将一个变量增加一。这就是我一直在尝试的:

@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;
    }
}
基本上,我要做的是一个和两个孩子,得到一个颜色值,三个和四个孩子得到另一个,五个和六个孩子得到第三个。你知道为什么这样不行吗?非常感谢您的帮助。

在您的代码中(假设您粘贴了完整的代码),您直接使用了
&:nth-child()
,这将产生错误
基本级别规则不能包含引用字符“&”的父选择器。
您需要为其指定父选择器

$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;
  }
}
HTML


输出:


非常感谢您花时间解释这一点,我真的很感激。只是学习这一点,有时你只是需要一个轻推或概念解释。
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>