如何在jQuery中停止效果

时间:2020-03-05 18:52:31  来源:igfitidea点击:

我有一个页面使用

$(id).show("highlight", {}, 2000);

在我启动ajax请求时突出显示一个元素,可能会失败,因此我想使用类似

$(id).show("highlight", {color: "#FF0000"}, 2000);

在错误处理程序中。问题在于,如果第一个高光尚未完成,则第二个高光将被放入队列中,直到第一个高光准备好后才能运行。因此出现了一个问题:我能以某种方式停止第一个作用吗?

解决方案

回答

从jQuery文档中:

http://docs.jquery.com/Effects/stop

Stop the currently-running animation on the matched elements....
  
  When .stop() is called on an element, the currently-running animation (if any) is immediately stopped. If, for instance, an element is being hidden with .slideUp() when .stop() is called, the element will now still be displayed, but will be a fraction of its previous height. Callback functions are not called.
  
  If more than one animation method is called on the same element, the later animations are placed in the effects queue for the element. These animations will not begin until the first one completes. When .stop() is called, the next animation in the queue begins immediately. If the clearQueue parameter is provided with a value of true, then the rest of the animations in the queue are removed and never run.
  
  If the jumpToEnd argument is provided with a value of true, the current animation stops, but the element is immediately given its target values for each CSS property. In our above .slideUp() example, the element would be immediately hidden. The callback function is then immediately called, if provided...