0

实际上回调函数是在动画完成后调用的

(selector).animate({styles},speed,easing,callback)

如何在动画开始之前调用回调函数?

或者像这样同时运行动画时如何调用回调函数?

4

2 回答 2

1

ifcallback是一个函数,之前调用它animate

callback();
(selector).animate({styles},speed,easing,callback)
于 2013-09-11T06:48:09.293 回答
0
function animate(preCallback, postCallback) {

    // Create var to hold animating object if you want to use deffereds
    var animation;

    // Animation is about to start run preCallback()
    // If you don't want the animation to start until this is complete
    // You should look at returning a deffered object from the callback
    // or using another callback inside, warning : this can get messy.
    preCallback instanceof Function && preCallback();

    // Start the animation
    animation = $('#animation').animate({styles}, speed, easing, function() {
        // Once animation is finished run finished callback
        postCallback instanceof Function && postCallback();
    });

    // Alternative Return animation as deffered so further 
    // complete callbacks can be chained to it with .done()
    return $.when(animation);
}

animate(function() {
    // preCallback code, this will NOT stop animation from starting
    // before it is complete without further logic
}, function() { 
    // postCallback code
}).done(function() {
    // further callback using deffered object
});

请注意,preCallback最好先运行此代码,然后在完成后从中调用 animate,从而完全无需添加此功能。

于 2013-09-11T06:53:05.237 回答