取消所有排队的 jQuery SlideUp 和 SlideDown 动画
我有一个界面,它大量使用 jQuery SlideUp 和 SlideDown 效果以三态方式展开项目。
onmouseover: function() {
this.find('.details', this).slideDown();
},
onmouseout: function() {
this.find('.details', this).slideUp();
}
然而,当用户将鼠标快速移动到这些界面元素上时,动画无法跟上,并且在鼠标离开界面区域很长时间后,项目将上下滑动。
当鼠标离开项目的容器 div 时,有没有办法取消所有排队的幻灯片动画?
I have an interface that makes heavy use of the jQuery slideUp and slideDown effect to expand items in a tri-state kind of way.
onmouseover: function() {
this.find('.details', this).slideDown();
},
onmouseout: function() {
this.find('.details', this).slideUp();
}
However, when the user quickly moves the mouse over these interface elements the animations can't keep up and the items will be sliding up and down long after the mouse has left the interface area.
Is there a way to cancel all the queued-up slide animations when the mouse leaves the item's container div?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您真正想要的答案是所有其他三个答案的组合。
您希望停止时为 true,因为它们会清除待处理的动画队列。如果您不使用这些,您会发现在元素上快速且重复地移动鼠标会产生错误的结果。
The answer you really want is a combination of all the other three answers.
You want the
true
s in the stop because these clear the pending animation queues. If you don't use these, you'll find moving the mouse quickly and repeatedly across the element will produce buggy results.一般来说,您需要在启动此类动画时调用
stop()
:这应该足以避免长时间运行的动画队列。
您还可以使用
$.clearQueue()
全局清除尚未设置的动画还没有开始。另外,如果您在
mouseover()
和mouseout()
上设置这些,那么简单地使用hover()
事件。Generally speaking you want to call
stop()
when starting such an animation:That should be sufficient to avoid long-running animation queues.
You can also use
$.clearQueue()
to globally clear animations that haven't yet begun.Also, if you're setting these on
mouseover()
andmouseout()
it is arguably clearer to simply use thehover()
event instead.如果将参数放在
stop()
中也会更好,就像这样:stop(true,true)
...It is also much better if you put parameters in
stop()
, just like this:stop(true,true)
...就我而言,我也在寻找一个
.stop()
解决上下扩展动画队列的问题。不过还是没解决,因为不顺畅,还有bug,让它再也滑不下去了。因此,我提出了一个与取消队列无关的解决方案,但它可能会对你们中的一些人有所帮助。解决方案是在动画目标当前未进行动画处理时向下或向上滑动它。
In my case, I was also looking for a
.stop()
solution to the up and down extensive animation queue. However, it still didn't solve, because it wasn't smooth, and it was buggy, making it not to slide down anymore.Hence, I came with a solution that is not related to cancel queues, but it might help some of you. The solution is about sliding it down or up just when the animation target is not currently being animated.
您可以执行以下操作: http://jsfiddle.net/3o2bsxo6/3/
JavaScript
html:
CSS:
You could do something like the following: http://jsfiddle.net/3o2bsxo6/3/
JavaScript
html:
CSS:
类似的查询已发布在此线程中。
使用
you can achieve the effect without bugs.
我假设在 #YourDiv 之后有一个元素要放置向上滑动和向下滑动动画。
这将跳转到最后一个事件的动画并清除链中所有待处理的事件。
Similar query was posted in this thread.
Using
you can achieve the effect without bugs.
I've assumed that there is an element after #YourDiv that you want to put slide-up and slide-down animations.
This will jump to the animation of the last event and clear all the pending events in the chain.
我相信你应该能够只添加一个
.stop()
它会为你处理这个问题:I believe you should be able to just add a
.stop()
and it'll take care of that for you: