2
I need that when one animation ends in jQuery, another begins. And this should be done in sequence (without running at the same time).
I saw it on a project where I work a code that does this, but I think it has a lot of callbacks.
var speed = 1000;
var cb = undefined;
$(function() {
$('.item:eq(0)').animate({
opacity: 1
}, speed, function() {
$('.item:eq(1)').animate({
opacity: 1
}, speed, function() {
$('.item:eq(2)').animate({
opacity: 1
}, speed, function() {
$('.item:eq(0)').animate({
opacity: 0
}, speed, function() {
$('.item:eq(1)').animate({
opacity: 0
}, speed, function() {
$('.item:eq(2)').animate({
opacity: 0
}, speed, function() {
$.isFunction(cb) && cb();
})
})
})
})
})
})
});
.item {
height: 30px;
background: red;
opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
<li class="item">item</li>
<li class="item">item</li>
<li class="item">item</li>
</ul>
In that case, if we had more than 3 items (suppose we had 20), I wouldn’t want to have to put 20 callbacks.
Questions are:
- How could this code improve?
- How can I do these sequential animations in jQuery (one waiting for the other)?
In this specific example I would do like this: http://jsfiddle.net/whoc1tpd/ -> this is what you are looking for?
– Sergio
You can answer like this, you get +1. That’s just what I need
– Wallace Maxters