2013-06-27 62 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