如何使用SCSS/SASS增加并发div的动画延迟

2ne*_*2ne 15 css sass css3

我试图复制Windows 8磁贴的加载动画.看起来每个图块的动画延迟都略高于其前一个图块.我一直在以低效的方式使用nth-child来实现这一目标,如下所示.有没有人知道我能以更有效的方式实现这一目标,以满足任何数量的div?

DEMO

.results div:nth-child(1) {
  animation-delay: 0.25s;
}
.results div:nth-child(2) {
  animation-delay: 0.5s;
}
.results div:nth-child(3) {
  animation-delay: 0.75s;
}
.results div:nth-child(4) {
  animation-delay: 1s;
}
.results div:nth-child(5) {
  animation-delay: 1.25s;
}
.results div:nth-child(6) {
  animation-delay: 1.5s;
}
.results div:nth-child(7) {
  animation-delay: 1.75s;
}
.results div:nth-child(8) {
  animation-delay: 2s;
}
Run Code Online (Sandbox Code Playgroud)

DMT*_*ner 43

您可以在Sass中使用for循环来执行此操作:

@for $i from 1 to 10 {
  .results div:nth-child(#{$i}) { animation-delay: $i * 0.25s; }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过将10设置为您需要的任何数量来实现任意数量的div.


xet*_*h23 15

@for $i from 1 through 10 {
  .results div:nth-child(#{$i}) {
    -webkit-animation-delay:(#{$i*0.1s}); 
    animation-delay:(#{$i*0.1s}); 
  }
}
Run Code Online (Sandbox Code Playgroud)

更好的解决方案......我测试过它有效;)

  • 为什么这是更好的解决方案? (5认同)
  • 似乎“通过”包括“ 10”,但是“至”排除 (2认同)

小智 6

我使用这个混合:

@mixin delay($rule, $number, $value) {
  @for $i from 1 to ($number + 1) {
    &:nth-child(#{$i}) {
      -webkit-#{$rule}-delay: (#{$i*$value});
      #{$rule}-delay: (#{$i*$value});
    }
  }
}

.results div{
  @include delay(animation, 8, 0.25s);
}
Run Code Online (Sandbox Code Playgroud)

这样你也可以重新使用它的过渡。