对每个子元素具有延迟的 CSS 动画

我试图通过对每个子元素应用动画来创建级联效果。我想知道是否有比这更好的方法:

.myClass img:nth-child(1){
-webkit-animation: myAnimation 0.9s linear forwards;
}
.myClass img:nth-child(2){
-webkit-animation: myAnimation 0.9s linear 0.1s forwards;
}
.myClass img:nth-child(3){
-webkit-animation: myAnimation 0.9s linear 0.2s forwards;
}
.myClass img:nth-child(4){
-webkit-animation: myAnimation 0.9s linear 0.3s forwards;
}
.myClass img:nth-child(5){
-webkit-animation: myAnimation 0.9s linear 0.4s forwards;
}

诸如此类。 所以基本上,我想有一个动画开始为每个孩子,但有一个延迟。 谢谢你的建议!

补充: 也许我没有正确地解释我关心的是什么: 这是关于如何做到这一点,无论我有多少孩子。如何做到这一点,而不必写下每个孩子的属性... ... 例如,当我不知道有多少孩子将是。

121290 次浏览

Like this:

.myClass img {
-webkit-animation: myAnimation 0.9s linear forwards;
}


.myClass img:nth-child(1){
-webkit-animation-delay: 0.1s;
}


.myClass img:nth-child(2){
-webkit-animation-delay: 0.2s;
}


[...etc...]

What you want is the animation delay property.

@keyframes FadeIn {
0% {
opacity: 0;
transform: scale(.1);
}


85% {
opacity: 1;
transform: scale(1.05);
}
100% {
transform: scale(1);
}
}


.myClass img {
float: left;
margin: 20px;
animation: FadeIn 1s linear;
animation-fill-mode: both;
}


.myClass img:nth-child(1) { animation-delay: .5s }
.myClass img:nth-child(2) { animation-delay: 1s }
.myClass img:nth-child(3) { animation-delay: 1.5s }
.myClass img:nth-child(4) { animation-delay: 2s }
<div class="myClass">
<img src="http://placehold.it/200x150" />
<img src="http://placehold.it/200x150" />
<img src="http://placehold.it/200x150" />
<img src="http://placehold.it/200x150" />
</div>

A CSS preprocessor such as Less.js or Sass can reduce the amount of repetition, but if you're working with an unknown number of child elements or need to animate a large number then JavaScript will be the best option.

If you have a lot of items (for example: I have paginated table with >1000 items and wanna each row to be animated with delay when page is loads), you can use jQuery to solve this and avoid css file rising in size. Animation delay dynamically increases.

$.each($('.myClass'), function(i, el){
$(el).css({'opacity':0});
setTimeout(function(){
$(el).animate({
'opacity':1.0
}, 450);
},500 + ( i * 500 ));


});​

EDIT: Here is the same code I adjusted to use with animate.css (install additional plugin before use https://gist.github.com/1438179 )

$.each($(".myClass"), function(i, el){
$(el).css("opacity","0");
setTimeout(function(){
$(el).animateCSS("fadeIn","400");
},500 + ( i * 500 ));


});

Where "fadeIn" is animation type, "400" - animation execute time, 500 - delay for each element on page to be animated.

In the [hopefully near] future when attr and calc are fully supported, we'll be able to accomplish this without JavaScript.

HTML:

<ul class="something">
<li data-animation-offset="1.0">asdf</li>
<li data-animation-offset="1.3">asdf</li>
<li data-animation-offset="1.1">asdf</li>
<li data-animation-offset="1.2">asdf</li>
</ul>

CSS:

.something > li
{
animation: myAnimation 1s ease calc(0.5s * attr(data-animation-offset number 1));
}

This would create an effect where each list item animates in what would appear to be random order.

You can also make use of the transition-delay property in CSS and use JS or JQuery to assign a different delay for each child element . ( Assume s to be the starting delay in seconds )

$(".myClass img").each(function(index){
$(this).css({
'transition-delay' : s*(1+index) + 's'
});
});

So, the children will have the transition delays like 1*s, 2*s, 3*s ..... and so on. Now to create the actual animation effect simply set the required transition and the children will be animated in a sequence. Works like a charm !

Here's a scss way to do it using a for loop.

@for $i from 1 through 10 {
.myClass img:nth-child(#{$i}n) {
animation-delay: #{$i * 0.5}s;
}
}