本文介绍了@each索引循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否可以为@each循环获取元素索引。
I was wondering if you can get an element index for the @each loop.
我有以下代码,但我想知道 $ i
变量是执行此操作的最佳方法。
I have the following code, but I was wondering if the $i
variable was the best way to do this.
当前代码:
$i: 0;
$refcolors: #55A46A, #9BD385, #D9EA79, #E4EE77, #F2E975, #F2D368, #F0AB55, #ED7943, #EA4E38, #E80D19;
@each $c in $refcolors {
$i: $i + 1;
#cr-#{$i} strong {
background:$c;
}
}
推荐答案
第一个总之, @each
函数不是来自Compass,而是来自Sass。
First of all, the @each
function is not from Compass, but from Sass.
要回答您的问题,不能通过每个循环来完成,但是很容易将其转换为循环,可以这样做:
To answer your question, this cannot be done with an each loop, but it is easy to convert this into a @for
loop, which can do this:
@for $i from 1 through length($refcolors) {
$c: nth($refcolors, $i);
// ... do something fancy with $c
}
这篇关于@each索引循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!