我正在玩sass中的mixins
和for
循环,想问一句处理这种情况的最佳方法是什么:我在UI上有类,可以是基于名称的类或基于索引。因此,让我们开始编写代码。
classname = {cn('stepper',{step-${activeIndex}
})}
解决方案1:
$steps: (
0: 0%,1: 33%,2: 66%,3: 99%,);
@mixin step-modifiers {
@each $name,$hex in $steps {
&.step-#{$name} {
width: $hex;
}
}
}
.stepper{
@include step-modifiers;
}
解决方案2:
$steps: 3;
@for $i from 0 through $steps {
.step-#{$i} {
width: (100% * $i / $steps);
}
}
预期结果
.stepper.step-0 {
width: 0%;
}
.stepper.step-1 {
width: 33%;
}
.stepper.step-2 {
width: 66%;
}
.stepper.step-3 {
width: 99%;
}