[英]Generate a dynamic SCSS variable by appending another variable
I'm trying to assign different colors to similar items using an SCSS @for
loop.我正在尝试使用 SCSS @for
循环将不同的 colors 分配给类似的项目。 Can I append the $i
variable used in the @for
loop to $color-
?我可以将@for
循环中使用的$i
变量 append 转换为$color-
吗?
<div>
<h1>Hello</h1>
<h1>World</h1>
<h1>Goodbye</h1>
</div>
$color-1: red;
$color-2: blue;
$color-3: yellow;
@for $i from 1 to 3 {
div>h1:nth-child(#{$i}) {
color: $color-{$i};
}
}
I don't know about dynamic variable names, but the standard way to achieve what you want is SCSS lists, over which you can iterate.我不知道动态变量名称,但实现您想要的标准方法是 SCSS 列表,您可以对其进行迭代。
$colors-list: red blue yellow;
@each $current-color in $colors-list {
$i: index($colors-list, $current-color);
div>h1:nth-child(#{$i}) {
color: $current-color;
}
}
which compiles to编译为
div > h1:nth-child(1) {
color: red;
}
div > h1:nth-child(2) {
color: blue;
}
div > h1:nth-child(3) {
color: yellow;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.