开发者

Single queue for jQuery animate() elements

开发者 https://www.devze.com 2023-03-04 08:58 出处:网络
By default the jQuery queue that is created for animate() is done per element, I\'m wondering if there is a way to cr开发者_Python百科eate a single queue for all animations done with animate()? I.e. o

By default the jQuery queue that is created for animate() is done per element, I'm wondering if there is a way to cr开发者_Python百科eate a single queue for all animations done with animate()? I.e. only one animation should be occuring at a time


You could do it with your own custom queue on one element using queue:

http://jsfiddle.net/jRawX/2/

$(function(){

    $('#myQueue')
        .queue('myQueue',function(next){
            $('#t1').animate({left: 100}, 
                            {duration: 1000, 
                             queue: false,
                             complete: next
                            })
        })
        .queue('myQueue',function(next){
            $('#t2').animate({left: 100}, 
                            {duration:1000, 
                             queue:false,
                             complete: next})
        })
        /* etc. */
        .dequeue('myQueue')
})


.animate() has a queue option that will only allow one effect per element:

queue: A Boolean indicating whether to place the animation in the effects queue. If false, the animation will begin immediately.

Usage

$('div').animate({ height: 50, queue: false });


Something like this:

$(someElement) // could also be a plain object, e.g. $({})
    .queue('customQueue', function (next) {
        first.animate({ ... }, function () {
            // when the animation is complete, call next() for customQueue
            next();
        });
    })
    .queue('customQueue', function (next) {
        second.animate({ ... }, function () {
            // when the animation is complete, call next() for customQueue
            next();
        });
    })
    // add more animations, then
    .dequeue('customQueue');
0

精彩评论

暂无评论...
验证码 换一张
取 消