在SCSS中迭代变量?

Lia*_*iam 0 html css sass css3 scss-mixins

我正在编写一个函数来依次淡化项目...

.sequenced-images li:nth-child(2) {
  animation-delay: 1s;
}
.sequenced-images li:nth-child(3) {
  animation-delay: 2s;
}
.sequenced-images li:nth-child(4) {
  animation-delay: 3s;
}
.sequenced-images li:nth-child(5) {
  animation-delay: 4s;
}
Run Code Online (Sandbox Code Playgroud)

我有很多项目,我不想手动为下一个项目添加一个类.

我可以使用类似的东西迭代相同的规则吗?

.sequenced-images li:nth-child(i++) {
  animation-delay: i++s;
}
Run Code Online (Sandbox Code Playgroud)

juz*_*aai 6

是的,你可以使用for循环字符串插值:

@for $i from 2 through 5 {
  .sequenced-images li:nth-child(#{$i}) {
    animation-delay: '#{$i - 1}s';
  }
}
Run Code Online (Sandbox Code Playgroud)

这将导致:

.sequenced-images li:nth-child(2) {
  animation-delay: "1s";
}

.sequenced-images li:nth-child(3) {
  animation-delay: "2s";
}

.sequenced-images li:nth-child(4) {
  animation-delay: "3s";
}

.sequenced-images li:nth-child(5) {
  animation-delay: "4s";
}
Run Code Online (Sandbox Code Playgroud)