21

我正在尝试复制 Windows 8 磁贴的加载动画。似乎每个图块都有一个动画延迟,略高于其前身。我目前一直在以一种低效的方式使用 nth-child 来实现这一点,如下所示。有谁知道我可以以更有效的方式实现这一目标,以满足任意数量的 div 的需求?

演示

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

3 回答 3

57

您可以在 Sass 中使用 for 循环来执行此操作,如下所示:

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

然后,您可以通过将 10 个设置为您需要的任何 div 来为任意数量的 div 执行此操作。

于 2013-10-26T21:49:40.603 回答
15
@for $i from 1 through 10 {
  .results div:nth-child(#{$i}) {
    -webkit-animation-delay:(#{$i*0.1s}); 
    animation-delay:(#{$i*0.1s}); 
  }
}

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

于 2014-03-30T02:10:27.937 回答
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);
}

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

于 2019-04-05T14:59:42.973 回答