Single queue for jQuery animate() elements

后端 未结 3 1184
失恋的感觉
失恋的感觉 2021-01-21 02:53

By default the jQuery queue that is created for animate() is done per element, I\'m wondering if there is a way to create a single queue for all animations done with animate()?

相关标签:
3条回答
  • 2021-01-21 03:06

    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 讨论(0)
  • 2021-01-21 03:14

    .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 });
    
    0 讨论(0)
  • 2021-01-21 03:20

    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')
    })
    
    0 讨论(0)
提交回复
热议问题