【发布时间】:2019-09-11 14:48:47
【问题描述】:
我需要有关 scss mixin 的帮助。我正在尝试通过在 mixin 中使用 for 循环来使用 nth:child() 选择器添加动画延迟。每个孩子的延迟应该以 0.5 秒的增量增加。
li {
list-style: none;
transform: translateX(100rem);
animation: slideIn .5s forwards;
&:nth-child(1) {
animation-delay: 0s;
}
&:nth-child(2) {
animation-delay: .5s;
}
&:nth-child(3) {
animation-delay: 1s;
}
&:nth-child(4) {
animation-delay: 1.5s;
}
}
我已经用 mixin 替换了原来的 scss。请注意,上面的第一个孩子的动画延迟为 0s。
@mixin delay {
@for $i from 1 through 4 {
&:nth-child(#{$i}) {
animation-delay: $i * (0.5s);
}
}
}
最终代码。
li {
list-style: none;
transform: translateX(100rem);
animation: slideIn .5s forwards;
@include delay;
}
这很好用,只是它给第一个孩子增加了延迟。我该如何重写这个 mixin,让它跳过第一个孩子并从第二个孩子开始?
【问题讨论】:
-
@for $i from 2 through 4 -
我试过了,但它没有按预期执行。它只是给第二个孩子增加了更长的延迟。
标签: sass scss-mixins