错开CSS3动画时间

Jon*_*Jon 8 animation css3

我有很多.lines,我正在应用一个淡入淡出的动画.我想知道是否有一种简单的方法以某种方式错开动画时间,因此每一个都是一个接一个地触发(一次反对所有).换句话说,我想创造一个效果,第一个.line开始褪色,然后一秒钟后第二个.line会褪色,然后一秒钟后第三个.line会褪色.这只需要在Chrome中运行.

div.line { width: 300px; height:5px; background:red;
           -webkit-animation: fade 20s infinite;
           -webkit-animation-timing-function: linear;
}

@-webkit-keyframes fade {
    0%   { opacity:1.0; }
    50%  { opacity:0.0; }
    100% { opacity:1.0; }
}


<div class="line"></div>
<div class="line"></div>
...
<div class="line"></div>
Run Code Online (Sandbox Code Playgroud)

Ian*_*unn 11

使用nth-child选择每个单独的行,然后应用动画延迟,如下所示:

.line:nth-child(1) {
    -webkit-animation-delay: 1s;
}
.line:nth-child(2) {
    -webkit-animation-delay: 2s;
}
Run Code Online (Sandbox Code Playgroud)


Mow*_*zer 6

SASS解决方案可能看起来像这样:

这是CodePen

styles.css
@import "compass/css3";

.my-element {
    @include transition(opacity 0.3s);
    @include opacity(0);
    @include inline-block;
    margin: 5px;
    width: 100px;
    height: 100px;
    background: tomato;

    // Loop through the items and add some delay.
    @for $i from 1 through 3 {
            &:nth-child(#{$i}) { 
             @include transition-delay(0.1s * $i); 
            }
        }

    .my-container:hover & {
        @include opacity(1);
    }
}
Run Code Online (Sandbox Code Playgroud)

http://codepen.io/anon/pen/tngHy