2013-06-27 67 views
0

我想停止從動畫中滾動一旦匹配特定條件。一旦它符合某些條件就停止動畫 - jquery

這裏我有

var $box2 = $('.column'), 
    Width = max_x, 
    cursor = 0; 
var Width = $("#column").width(); 
console.log(divWidth) 


$("#right-button").click(function() { 
    if (cursor != Width && Width >= divWidth) { 
     $box2.animate({ 
      marginLeft: "-=100" 
     }, "fast"); 

     cursor += 300; 
    } 
}); 

我的動畫滾動到 - 0R 300取決於點擊。我想在這裏寫一個條件,就像當前寬度大於總寬度,停止動畫。

嘗試與if cursor != totalWidth && totalWidth >= divWidth && this.width > totalWidth,但無法正常工作。如何在動畫時查找當前寬度?我應該在這裏做什麼?

+1

請發表您的代碼操縱的HTML片段。 –

回答

0

嘗試使用步驟回調:

$box2.animate({ 
    marginLeft: "-=100" 
}, { 
    step: function (now, fx) { 
     if (cursor != totalWidth && totalWidth >= divWidth) $(this).stop(); 
    } 
}, "fast"); 
+0

不是,它不工作...我仍然可以在當前寬度後滾動。 – Ash

+0

你應該在jsfiddle中發佈你的相關代碼,很難看到你在找什麼...... –

+0

好的。只是保持簡單。我需要滾動一些寬度(totalWidth's寬度)。任何方法來做到這一點..?跟蹤寬度每次保證金去-100 .. – Ash