How can I make the animations be run one by one?

梦想的初衷 提交于 2019-12-18 04:36:07

问题


The HTML code:

<ul>
    <li>a</li>
    <li>b</li>
    <li>c</li>
    <li>d</li>
    <li>e</li>
</ul>

The js code:

<script type="text/javascript">
$(document).ready(function() {
    var lis = $("li");
    for (var i = 0; i < lis.length; i++) {
        $(lis[i]).animate({opacity: 0}, 1000);
    }
});
</script>

I just find that the lis will disappear together. How can I do that they will disappear one by one?


回答1:


Sorry for joining the party late.

The existing answers have issues when a timer blocks and rely on the accuracy of timers, moreover they require an actual delay.

jQuery actually provides a generic way to perform animations sequentially with promises:

$(document).ready(function() {
    var lis = $("li");
    var queue = $.Deferred().resolve(); // create an empty queue
    lis.get().forEach(function(li){
        queue = queue.then(function(){
           return $(li).animate({opacity: 0}, 1000).promise();
        })
    });
});

Fiddle

Note that this will work even if you assign them different animation durations or different animations, the result in queue will contain a hook on when the last animation finished. This also supports aggregations (waiting for all promises to finish in parallel) and more.




回答2:


Each element has its own animation queue, so as you've seen they all animate at the same time rather than waiting for the previous element to finish. You can add a delay for each element:

$(lis[i]).delay(i*1000).animate({opacity: 0}, 1000);
// ------^^^^^^^^^^^^^^

Demo: http://jsfiddle.net/p2kdpxr3/




回答3:


use delay()

$(lis[i]).delay(i * 500).animate({opacity: 0}, 1000);

DEMO




回答4:


You can use delay() function

for (var i = 0; i < lis.length; i++) {
    $(lis[i]).delay(i * 400).animate({opacity: 0}, 1000);
              ^^^^^^^^^^^^^^
}

JS Fiddle




回答5:


You could instead use CSS animation/transition and relevant transitionEnd event with following logic:

$(function() {
  var events = [
    "webkitTransitionEnd",
    "oTransitionEnd",
    "otransitionend",
    "MSTransitionEnd",
    "transitionend"
  ];


  $("li").on(events.join(" "), function(e) {
    $(this).next().addClass('animated');
  }).first().addClass('animated');
});
li {
  opacity: 1;
  -webkit-transition: opacity 1s;
  -moz-transition: opacity 1s;
  -ms-transition: opacity 1s;
  -o-transition: opacity 1s;
  transition: opacity 1s;
}
li.animated {
  opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<ul>
  <li>a</li>
  <li>b</li>
  <li>c</li>
  <li>d</li>
  <li>e</li>
</ul>

-DEMO jsFiddle-



来源:https://stackoverflow.com/questions/27653482/how-can-i-make-the-animations-be-run-one-by-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!