gpt4 book ai didi

for-loop - sass if for循环中的偶数

转载 作者:行者123 更新时间:2023-12-02 21:21:06 25 4
gpt4 key购买 nike

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

@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;
}
}

基本上,我想做的是让一号和二号 child 获得一个颜色值,三号和四号 child 获得另一个颜色值,五号和六号 child 获得第三个颜色值。知道为什么这不起作用吗?非常感谢您的帮助。

最佳答案

在您的代码中(假设您已粘贴完整的代码),您直接使用 &: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>

输出:

enter image description here

Demo here

关于for-loop - sass if for循环中的偶数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27648567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com