【问题标题】:Iterate variables in SCSS?在 SCSS 中迭代变量?
【发布时间】:2018-08-13 22:37:08
【问题描述】:

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

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

?

【问题讨论】:

标签: html css sass scss-mixins


【解决方案1】:

是的,您可以使用for loopstring interpolation

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

【讨论】:

  • 太好了,谢谢!这是否意味着每次添加项目时,我都需要更新“2 到 5”,或者我可以使用通配符,以便我可以根据需要添加任意项目?
  • 不客气! :) 你必须更新循环头中的数字,我想不出通配符解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 2015-12-01
  • 2010-11-17
  • 1970-01-01
  • 2013-08-03
  • 1970-01-01
  • 2014-06-06
相关资源
最近更新 更多