简体   繁体   English

jQuery +停止+回调函数

[英]jQuery + Stop + Callback Functions

From what I understand using .stop() will prevent the callback functions to fire, right? 据我了解,使用.stop()可以防止触发回调函数,对吗?

Unfortunately, it seems that this does not work in my current script: 不幸的是,这似乎在我当前的脚本中不起作用:

newSubMenu.stop().slideToggle(250, function() {
  newSubMenu.css('visibility', 'visible').addClass('open');
});

The animation now stops when double clicking, but newSubMenu still gets the class open and I cant figure out why. 现在,双击动画会停止,但是newSubMenu仍然使类open ,我无法弄清原因。

The goal I am trying to achieve, is to NOT apply the class open until the animation is complete. 我试图实现的目标是在动画完成之前不应用open类。

From the documentation : 文档

When .stop() is called on an element, the currently-running animation (if any) is immediately stopped. 在元素上调用.stop() ,当前正在运行的动画(如果有)立即停止。 … Callback functions are not called. …不调用回调函数。

Callback functions are called if the third argument to stop() is true . 如果stop()的第三个参数为true 调用回调函数。

In the code you provide, though, the callback will run because you're stopping already running animations, not the slideToggle() animation which has yet to run when stop() is called. 但是,在您提供的代码中,将运行回调,因为您将停止已经在运行的动画,而不是在slideToggle() stop()时尚未运行的slideToggle()动画。

Here is a working example showing the callback being stopped . 这是一个显示回调已停止工作示例

To see why this happens, you have to understand, what toggle does. 要了解为什么会发生这种情况,您必须了解切换的作用。 Everytime you click, the slideToggle gets itself the information to slideDown or slideUp . 每次单击时, slideToggle都会将自身的信息获取到slideDownslideUp

The conclusion is: everytime the toggle is complete, your function will be called and your newSubMenu gets the visibility:visible style , plus the class "open" if it doesn't exist. 结论是:每当切换完成时,就会调用您的函数,并且newSubMenu将获得visibility:visible 样式 ,如果不存在该类,则添加类"open"

Click-> Stop all animations on element -> toggle slide -> call/excecute function

.stop() can be used to stop currently running animations. .stop()可用于停止当前正在运行的动画。 Once the animation has been completed the callback will be executed. 动画完成后,将执行回调。 In order to stop the current animation and avoid the callback being called you need to do something like this; 为了停止当前动画并避免调用回调,您需要执行以下操作:

newSubMenu.mouseover(function(){
    newSubMenu.slideToggle(250, function(){
        $(this).css('visibility', 'visible').addClass('open');
    });
}).dblclick(function(){

    // this will stop the execution of the .slideToggle animation
    // whitch will prevent the callback from being executed
    newSubMenu.stop();

});

jsFiddle example jsFiddle例子

jQuery has added an "always" callback in version 1.8: jQuery在版本1.8中添加了“始终”回调:

always 总是

Type: Function( Promise animation, Boolean jumpedToEnd ) 类型:函数(Promise动画,布尔型jumpedToEnd)

A function to be called when the animation completes or stops without completing (its Promise object is either resolved or rejected). 动画完成或停止但未完成时调用的函数(其Promise对象已解析或拒绝)。 (version added: 1.8) (添加的版本:1.8)

URL: http://api.jquery.com/animate/ 网址: http//api.jquery.com/animate/

This will be fired always, if an animation is regularly done or if you interupt it with stop() . 如果定期制作动画或将其与stop()交互,则将始终触发该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM