2009-12-23 59 views

回答

1

我假設你想在屏幕上的DIV從一個位置移動到另一個。做到這一點的方法是鏈接幾個animate()調用,在前一步完成時將它從一個位置移動到另一個位置。也許將中間位置(如果有的話)存儲在一個數組中,然後從每個有效呼叫的下一個位置彈出,直到所有呼叫都完成。使用遞歸回調函數可能是處理這個問題的最好方法。

我假設DIV已經絕對定位了。如果沒有,你可能想看看makeAbsolute插件。

未經檢驗

var positions = [ [ x1, y1 ], [ x2, y2 ], .. ]; 

play($('#myDiv'), positions); 


function play(elem, positions) { 
    if (positions.length > 0) { 
     var position = positions.shift(); 
     elem.animate({ left: position[0], top: position[1] } }, function() { 
      play(elem, positions); 
     }); 
    } 
} 
1
$("#myDiv").click(
    function(event){ 
     $(this).animate({"left:0"},1500,'linear' // the fourth parameter is a callback function which runs when the animation completes 
      function(){ $(this).animate(...); 
    } 
); 

由於tvanfosson說的,這只是一系列調用animate函數的 - 上面的代碼是不完全理想的你在說什麼,但它足夠清楚地說明了這個例子(我認爲)。