取消所有排队的 jQuery SlideUp 和 SlideDown 动画

发布于 2024-08-23 18:51:26 字数 338 浏览 6 评论 0原文

我有一个界面,它大量使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

呆° 2024-08-30 18:51:27

您真正想要的答案是所有其他三个答案的组合。

$("...").hover(function() {
  $(this).stop(true, true).slideDown();
}, function() {
  $(this).stop(true, true).slideUp();
});

您希望停止时为 true,因为它们会清除待处理的动画队列。如果您不使用这些,您会发现在元素上快速且重复地移动鼠标会产生错误的结果。

The answer you really want is a combination of all the other three answers.

$("...").hover(function() {
  $(this).stop(true, true).slideDown();
}, function() {
  $(this).stop(true, true).slideUp();
});

You want the trues 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.

别挽留 2024-08-30 18:51:27

一般来说,您需要在启动此类动画时调用 stop()

$("...").hover(function() {
  $(this).stop().slideDown();
}, function() {
  $(this).stop().slideUp();
});

这应该足以避免长时间运行的动画队列。

您还可以使用 $.clearQueue() 全局清除尚未设置的动画还没有开始。

另外,如果您在 mouseover()mouseout() 上设置这些,那么简单地使用 hover() 事件。

Generally speaking you want to call stop() when starting such an animation:

$("...").hover(function() {
  $(this).stop().slideDown();
}, function() {
  $(this).stop().slideUp();
});

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() and mouseout() it is arguably clearer to simply use the hover() event instead.

心欲静而疯不止 2024-08-30 18:51:27

如果将参数放在 stop() 中也会更好,就像这样:stop(true,true)...

It is also much better if you put parameters in stop(), just like this: stop(true,true)...

春夜浅 2024-08-30 18:51:27

就我而言,我也在寻找一个 .stop() 解决上下扩展动画队列的问题。不过还是没解决,因为不顺畅,还有bug,让它再也滑不下去了。

因此,我提出了一个与取消队列无关的解决方案,但它可能会对你们中的一些人有所帮助。解决方案是在动画目标当前未进行动画处理时向下或向上滑动它。

$("#trigger").on({
    mouseover: function() {
        $("#animationTarget").not(":animated").slideDown();
    },
    mouseleave: function() {
        $("#animationTarget").not(":animated").slideUp();
    }
});

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.

$("#trigger").on({
    mouseover: function() {
        $("#animationTarget").not(":animated").slideDown();
    },
    mouseleave: function() {
        $("#animationTarget").not(":animated").slideUp();
    }
});
浅黛梨妆こ 2024-08-30 18:51:27

您可以执行以下操作: http://jsfiddle.net/3o2bsxo6/3/

JavaScript

$('h5').each(function(){
    $(this).unbind('mouseover'); //unbind previous events (prevent repeats)
    $(this).unbind('mouseout');

    $(this).on('mouseover',function(){
        if(!$(this).next('.details').is(':visible')){ //if not visible
             $(this).next('.details').slideDown();   //slidedown                        
        }
    })  
    $(this).on('mouseout',function(){
        if($(this).next('.details').is(':visible')){ //if visible
             $(this).next('.details').slideUp();    //slideup                           
        }
    })      
})

html:

<h5>Hover To Slide</h5>
<p class="details">
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.    
</p>
<h5>Hover To Slide</h5>
<p class="details">
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.    
</p>
<h5>Hover To Slide</h5>
<p>
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.    
</p>

CSS:

p{
    display:none;
}

You could do something like the following: http://jsfiddle.net/3o2bsxo6/3/

JavaScript

$('h5').each(function(){
    $(this).unbind('mouseover'); //unbind previous events (prevent repeats)
    $(this).unbind('mouseout');

    $(this).on('mouseover',function(){
        if(!$(this).next('.details').is(':visible')){ //if not visible
             $(this).next('.details').slideDown();   //slidedown                        
        }
    })  
    $(this).on('mouseout',function(){
        if($(this).next('.details').is(':visible')){ //if visible
             $(this).next('.details').slideUp();    //slideup                           
        }
    })      
})

html:

<h5>Hover To Slide</h5>
<p class="details">
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.    
</p>
<h5>Hover To Slide</h5>
<p class="details">
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.    
</p>
<h5>Hover To Slide</h5>
<p>
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.    
</p>

CSS:

p{
    display:none;
}
浅黛梨妆こ 2024-08-30 18:51:27

类似的查询已发布在线程中。
使用

停止(真,假)

you can achieve the effect without bugs.

$('#YourDiv').on('mouseenter', function(){
   $(this).next().slideDown(250).stop(true, false).slideDown()  
});

我假设在 #YourDiv 之后有一个元素要放置向上滑动和向下滑动动画。

这将跳转到最后一个事件的动画并清除链中所有待处理的事件。

Similar query was posted in this thread.
Using

stop(true, false)

you can achieve the effect without bugs.

$('#YourDiv').on('mouseenter', function(){
   $(this).next().slideDown(250).stop(true, false).slideDown()  
});

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.

一腔孤↑勇 2024-08-30 18:51:26

我相信你应该能够只添加一个 .stop() 它会为你处理这个问题:

onmouseover: function() { 
    this.find('.details', this).stop().slideDown(); 
},
onmouseout: function() { 
    this.find('.details', this).stop().slideUp(); 
}

I believe you should be able to just add a .stop() and it'll take care of that for you:

onmouseover: function() { 
    this.find('.details', this).stop().slideDown(); 
},
onmouseout: function() { 
    this.find('.details', this).stop().slideUp(); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文