Jquery 管理具有多个元素的队列

Jquery 管理具有多个元素的队列,jquery,queue,Jquery,Queue,我需要一个事件来启动,然后停止/清除多个元素的动画/动作队列。我是基于此创建的。这将为您提供一个良好的起点: CSS ​ HTML 祝你好运 div { width:40px; height:40px; } #one { background:green; } #two { background: red; } <button id="startQueue">Go</button> <button id="clearQueue"

我需要一个事件来启动,然后停止/清除多个元素的动画/动作队列。我是基于此创建的。

这将为您提供一个良好的起点:

CSS

​ HTML

祝你好运

div {
    width:40px;
    height:40px;
}

#one {
    background:green;
}

#two {
    background: red;
}
<button id="startQueue">Go</button>
<button id="clearQueue">Clear</button>

<div id="one" class="queueItem"></div>
<div id="two" class="queueItem"></div>​
// Global Queue Array //
var queue = [];

// Start Queue //
$('#startQueue').bind('click', function(e){
    // Go through Each Element and Fade Out //
    $('.queueItem').each(function(i, $el){
        // Don't Delay First Element//
        if (i==0) {
            queue.push($(this).fadeOut(5000));
        } else {
            queue.push($(this).delay(5000*i).fadeOut(5000));
        }        
    });
});

// Stop / Clear Queue //
$('#clearQueue').bind('click', function(e){
    $.each(queue, function(i, $el){
        // Stop Animation and Reset Elements //
        $el.stop(true, true).css({
            display: 'block',
            opacity: '1'
        });

        // Clear Queue Array //
        queue = [];
    });
});