CSS动画,每个子元素都有延迟

问题描述 投票:54回答:6

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

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

等等......所以基本上,我想为每个孩子制作一个动画,但是会有延迟。感谢您的任何意见!

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

css animation css3 delay css-selectors
6个回答
45
投票

你想要的是animation delay财产。

@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>

Less.jsSass这样的CSS预处理器可以减少重复次数,但是如果您使用的是未知数量的子元素或需要为大量动画制作动画,那么JavaScript将是最佳选择。


30
投票

这是使用for循环执行此操作的scss方法。

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

17
投票

在完全支持attrcalc的[希望接近]未来,我们将能够在没有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));
}

这将产生一种效果,其中每个列表项以看似随机的顺序动画。


16
投票

您还可以在CSS中使用transition-delay属性,并使用JS或JQuery为每个子元素分配不同的延迟。 (假设s是以秒为单位的起始延迟)

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

因此,孩子们将有1 * s,2 * s,3 * s等转换延迟等等。现在,要创建实际的动画效果,只需设置所需的过渡,子项将按顺序设置动画。奇迹般有效 !


8
投票

如果你有很多项目(例如:我有一个> 1000个项目的分页表,并且想要在页面加载时使每个行都有延迟动画),你可以使用jQuery解决这个问题并避免css文件大小上升。动画延迟动态增加。

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

});​

编辑:这是我调整为与animate.css一起使用的相同代码(在使用https://gist.github.com/1438179之前安装额外的插件)

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

});

其中“fadeIn”是动画类型,“400” - 动画执行时间,500 - 延迟页面上要动画的每个元素。


0
投票

像这样:

.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...]
© www.soinside.com 2019 - 2024. All rights reserved.