Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stagger CSS3 Animation Timing

Tags:

css

animation

I have a lot of .lines and I am applying a fading animation to them. I am wondering if there is a simple way to somehow stagger the animation timing so each one is triggered one after another (as oppose to all at once). In other words, I want to create an effect where the first .line would begin fading, then a second later the second .line would fade, then a second later the third .line would fade. This only needs to work in 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>
like image 733
Jon Avatar asked Dec 18 '12 23:12

Jon


People also ask

How do I delay an animation in CSS?

The CSS animation-delay property has the following syntax: animation-delay: [time] | initial | inherit; As you can see, there are three possible values: time, initial, and inherit. The first option is [time], which is the number of seconds or milliseconds before the animation starts.

What is staggering in animation?

A staggered animation consists of sequential or overlapping animations. To create a staggered animation, use multiple Animation objects. One AnimationController controls all of the Animation s.

What is CSS animation-timing-function?

The CSS animation-timing function sets the pace of the animation. Meaning, it allows you to control when the animation speeds up and slows down during the animation cycle. It's important to understand that this function does not affect the actual duration of an animation.

What is the default animation-timing-function?

default animation-timing-function: ease; The animation starts slowly, accelerates in the middle, and slows down at the end. animation-timing-function: ease-in; The animation starts slowly, and accelerates gradually until the end.


2 Answers

Use nth-child to select each individual line then apply an animation-delay, like so:

.line:nth-child(1) {
    -webkit-animation-delay: 1s;
}
.line:nth-child(2) {
    -webkit-animation-delay: 2s;
}
like image 96
Ian Lunn Avatar answered Oct 12 '22 19:10

Ian Lunn


A SASS solution might look something like this:

Here's the 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);
    }
}

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

like image 38
Let Me Tink About It Avatar answered Oct 12 '22 20:10

Let Me Tink About It