2011-08-29 53 views
2

背景如何防止JQuery使動畫調用排隊?

我對在很大程度上依賴於JQuery的.animate()的一個項目工作,偶爾通過Socket.io調用動畫()接收外部更新。當窗口打開時,一切運行正常,動畫呼叫可以異步運行。

問題

然而,當最小化瀏覽器或不同的選項卡打開,然後重新打開時,應同時關閉正在排隊和運行中,他們收到已運行的所有動畫。

這裏是生命的呼喚:

$(div).animate({ 
    'top': this.topPos(), 
    'right': this.rightPos() 
}, 100); 

有沒有一種簡單的方法來對選項添加到動畫()調用或調用一些jQuery函數或我應該只是添加相應的邏輯應用?

謝謝。

回答

4

使用.stop(true,true)通過傳遞true作爲這兩個參數

.stop([clearQueue], [jumpToEnd]) 

clearQueue: A Boolean indicating whether to remove queued animation as well. Defaults to false. 

jumpToEnd: A Boolean indicating whether to complete the current animation immediately. Defaults to false. 
在你的榜樣

它將被應用,如:

$(div).stop(true,true).animate({ 
    'top': this.topPos(), 
    'right': this.rightPos() 
}, 100); 
+2

什麼我建議 – vol7ron

+0

-1我很抱歉,但有用,因爲.stop(真實的,真實的)是,作爲@rick狀態,其原因是jQuery的requestAnimationFrame是usesd那造成了這個問題。 – Tomgrohl

+0

對不起,這個答案是100%不正確的。 – Rick

1

在您再次撥打animate()之前,您可以在正在動畫的元素上調用stop()。像這樣:

$(div).stop().animate({ 
    'top': this.topPos(), 
    'right': this.rightPos() 
}, 100); 

它將停止正在排隊的動畫,並一次性觸發。

here for details of stop()

希望這有助於。