gpt4 book ai didi

html - 如何查找列表中元素的 SCSS 索引?

转载 作者:行者123 更新时间:2023-12-05 00:48:07 28 4
gpt4 key购买 nike

我正在研究 SCSS 循环,但遇到了一个问题。问题是考虑颜色列表。

$colors:'red','green','red','blue';

@each $color in $colors{
$i:index($colors,$color);
.cube:nth-child(#{$i}){
background:$color;
};
}

上述程序的输出是

.cube:nth-child(1) {
background: "red";
}

.cube:nth-child(2) {
background: "green";
}

.cube:nth-child(1) { // issue here unable to get index value 3
background: "red";
}

.cube:nth-child(4) {
background: "blue";
}

我无法获得索引值 3。有人可以帮我解决这个问题吗?我的问题是

  1. 如何得到3的索引值?
  2. 是否可以使用 each 获取索引?如果"is"怎么办?
  3. 如果不是,还有什么替代方法?

最佳答案

这是你需要的:

$colors:'red','green','red','blue';

@for $i from 1 through length($colors) {
$color: nth($colors, $i);
.cube:nth-child(#{$i}){
background:$color;
};
}

您的失败原因 index($colors,$color) 将始终返回元素的第一个位置:Read -> http://sass-lang.com/documentation/Sass/Script/Functions.html#index-instance_method

关于html - 如何查找列表中元素的 SCSS 索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53155503/

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