简体   繁体   English

.animate()的jQuery自定义回调

[英]jQuery Custom Callback for .animate()

I'm trying to create a custom callback for the jQuery .animate() function. 我正在尝试为jQuery .animate()函数创建一个自定义回调。 I'm limited to jQuery 1.4.2 and based my custom call on this [article by Dan Switzer][1] 我仅限使用jQuery 1.4.2,并基于此[Dan Switzer的文章] [1]进行了自定义调用

(function ($){
    var oldAnimate = $.fn.animate;
    $.fn.animate = function(){
        this.trigger('animate.before', arguments);
        var animateResult = oldAnimate.apply(this, arguments);
        this.trigger('animate.after', arguments);
        return animateResult;
    };
})(jQuery || {});

$('#ID').bind('animate.after', function (){ 
    //Do Something
});

However when I run this code, my '//Do Something' does not trigger. 但是,当我运行此代码时,我的“ //做某事”不会触发。 I also tried following [Dave Ward's article][1] as well, using this: 我也尝试使用[Dave Ward的文章] [1]:

var oldAnimate = jQuery.animate;

jQuery.animate = function() {
    if (typeof animate.before === 'function')
    animate.before.apply(this, arguments);
    var animateResult = oldAnimate.apply(this, arguments);
    if (typeof animate.after === 'function')
    animate.after.apply(this, arguments);
    return animateResult;
};

I'm not sure where I'm going wrong. 我不确定我要去哪里。

Ok, so you've found that your code doesn't work. 好的,所以您发现您的代码无效。 Step one would be to simplify it and test the parts of it separately. 第一步将是简化它并分别测试它的各个部分。 Lets start with the event. 让我们从事件开始。

$("#ID").bind("animate.before animate.after",function(e){
   console.log(e.type); 
}).trigger("animate.before").trigger("animate.after");

This results in two events triggered with type equal to "animate" both times. 这将导致两个事件的两次触发类型均等于“动画”。 To make it say animatebefore and animate after, replace the . 要使其在之前设置为animatebefore,在之后设置为animate,请替换. with : :

$("#ID").bind("animate:before animate:after",function(e){
   console.log(e.type); 
}).trigger("animate:before").trigger("animate:after");

Now we properly get animate:before and animate:after . 现在我们正确地获得animate:beforeanimate:after Now that we know our event is working, lets tie that into the animate method. 现在我们知道我们的事件正在运行,让我们将其绑定到动画方法中。

$("#ID").bind("animate:before animate:after",function(e){
   console.log(e.type); 
});

var oldanim = $.fn.animate;
$.fn.animate = function() {
    this.trigger("animate:before");
    oldanim.apply(this,arguments);
    this.trigger("animate:after");
};

$("#ID").animate({"width":"200px"},2000,function(){
    console.log("animation complete");
});

It works!!! 有用!!! however, you'll notice pretty quickly that the after event is happing way later than it should be. 但是,您很快就会注意到,after事件的延迟时间比应该的晚。 This is because the animate method performs in an asynchronous way using setTimeouts, therefore the code continues to run. 这是因为animate方法使用setTimeouts以异步方式执行,因此代码继续运行。 I don't have any suggestions for you yet as far as how to fix that due to the fact that we don't have deferred objects until 1.5. 关于如何解决该问题,我还没有任何建议,因为我们直到1.5才将对象推迟。 You could override the complete function, but you'd have to take into account that it can be attached in two different ways. 您可以覆盖完整的功能,但必须考虑可以两种不同的方式附加该功能。

How about the complete option? complete选项怎么样? I guess this was available in jQuery 1.4.1 我猜这在jQuery 1.4.1中可用

$('#id').animate({
    properties..
}, {
    complete: function() {
        // doStuff
    }
});

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

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