$(function(){ // 定義一個叫做 myfun 的執行序列 // // 第一個參數不填的話,預設"fx"會自動執行 // 名稱設定 "fx" 無法執行 animate() // // 第二個參數,就放置依序要執行的 function。 // 官方解釋『An array of functions to replace the current queue contents.』 // 就是 『當前 function 執行結束前使用 dequeue() 再次呼叫,可以跳往下一個 function』 // 例如下面 『第一個動畫執行結束前使用 dequeue() 再次呼叫,可以跳往第二個動畫』 $(".box").queue("myfun", [ //第一個動畫 function (){ console.log("A-start"); $(".box").animate({ marginLeft: 300 }, 2000, function (){ console.log("A-end") //呼叫使用 dequeue() 會取代當前執行的 function, //也就是使用下一個function $(this).dequeue("myfun"); }) }, //第二個動畫 function (){ console.log("B-start"); $(".box").animate({ marginLeft: "-=200" }, 1000, function (){ console.log("B-end"); }) } ]); // 執行它 $(".box").dequeue("myfun"); })
<div class="box"> </div>
.box { width: 50px; height: 50px; background: black; float: left; }