0

我正在编写一个函数来按顺序淡入项目......

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

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

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

.sequenced-images li:nth-child(i++) {
  animation-delay: i++s;
}

?

4

1 回答 1

6

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

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

这将导致:

.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";
}
于 2018-08-13T14:29:53.173 回答