简体   繁体   English

CSS动画一个接一个地应用于元素?

[英]CSS Animation to apply to elements one after the other?

I'm trying to use the CSS animation in my project. 我正在尝试在我的项目中使用CSS动画。

everything works as it should. 一切都按预期工作。 However, when I run my code, the CSS animation get applied to all the elements with the class .allPro at the same time. 但是,当我运行我的代码时,CSS动画会同时应用于类.allPro所有元素。

Is there any way to apply the CSS animation individually? 有没有办法单独应用CSS动画?

For example, make the first element appear and then the second one and then third one and so on and so forth? 例如,让第一个元素出现,然后是第二个元素,然后是第三个元素,依此类推等等?

This is my code on FIDDLE 这是我在FIDDLE上的代码

And this is my CSS code: 这是我的CSS代码:

.allPro {
  width:150px;
  height:150px;
  float:left;
  margin:5px;
  background-color:#000;
  animation: fadein 2s;
}
@keyframes fadein {
  from { opacity: 0}
  to   { opacity: 1}
}

The elements are created dynamically. 元素是动态创建的。 So I cannot predict how many elements would be on the page. 所以我无法预测页面上会有多少元素。

I am willing to use jQuery to achieve what I'm trying to do. 我愿意使用jQuery来实现我想要做的事情。

EDIT: 编辑:

This is how I get the elements created dynamically on my page and append them to the container: 这就是我如何获取在页面上动态创建的元素并将它们附加到容器:

$(document).ready(function() {
  var poutput = $('.dotted-list');
  $.ajax({
    url: 'https://mydonain.com/page.php',
    dataType: 'jsonp',
    jsonp: 'jsoncallback',
    timeout: 5000,
    success: function(data, status) {
      $.each(data, function(pi, item) {
        str = item.name;
        if (str.length > 2) str = str.substring(0, 2)
        var products = '<a class="allPro" data-id="' + item.id + '" data-name="' + item.name + '" data-type="' + item.type + '" href="" data-transition="slidefade">' + '<div class="products-feed-Small">' + '<div style="position:absolute; top:40%; text-align:center; width:100%; color:#fff; font-family:Walpurga; font-size:28px; font-weight:bold; text-decoration: none;">' + item.name + '</div>'

        +'<img src="62.jpg" >' + '</div>' + '</a>';
        poutput.append(products);
        $('.allPro').each(function() {
          var delay = $(this).index();
          $(this).css('animation-delay', delay + 's');
        });
        //$( "#load-more" ).show();
      });
    },
    error: function() {
      poutput.text('There was an error loading the data.');
    }
  });
});

One way to do this with jQuery (or JavaScript) would be to find the index of the element and then set the animation-delay based on it like in the below snippet. 使用jQuery(或JavaScript)执行此操作的一种方法是查找元素的index ,然后根据它设置animation-delay ,如下面的代码段所示。

One key thing to note is that I've added backwards ( animation-fill-mode ) to the animation . 需要注意的一个关键问题是,我加backwardsanimation-fill-mode )的animation This is required because generally elements will remain in the default state until the animation's delay timer expires. 这是必需的,因为通常元素将保持默认状态,直到动画的延迟计时器到期为止。 In this case the default state is opacity: 1 (as there is no opacity setting on .allPro ). 在这种情况下,默认状态为opacity: 1 (因为.allPro上没有opacity设置)。 It means all elements become visible at once and then when the animation actually starts , they blink. 这意味着所有元素一次变为可见,然后当动画实际开始时 ,它们会闪烁。

By setting backwards as fill mode, we are instructing the UA that during the delay period the elements should hold the state as mentioned in the first keyframe. 通过backwards设置为填充模式,我们指示UA在延迟期间元素应该保持第一个关键帧中提到的状态。 This change means that the elements get the opacity: 0 setting as soon as they are appended (because animation is added in default selector). 此更改意味着元素一旦添加就会获得opacity: 0设置(因为animation是在默认选择器中添加的)。 So, they start to appear only when the animation is actually happening and not before it. 因此,它们只在动画实际发生时而不是在动画之前才开始出现。

 $(document).ready(function() { for (var i = 0; i < 10; i++) { $('body').append('<div class="allPro"></div>'); } $('.allPro').each(function() { var delay = $(this).index(); $(this).css('animation-delay', delay + 's'); }); }); 
 .allPro { width: 150px; height: 150px; float: left; margin: 5px; background-color: #000; animation: fadein 2s backwards; } @keyframes fadein { from { opacity: 0 } to { opacity: 1 } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM