実際には、アニメーションの完了後にコールバック関数が呼び出されます
(selector).animate({styles},speed,easing,callback)
アニメーションの開始前にコールバック関数を呼び出すにはどうすればよいですか?
または、アニメーションがその間に実行されているときに、このようにコールバック関数を呼び出すにはどうすればよいですか?
実際には、アニメーションの完了後にコールバック関数が呼び出されます
(selector).animate({styles},speed,easing,callback)
アニメーションの開始前にコールバック関数を呼び出すにはどうすればよいですか?
または、アニメーションがその間に実行されているときに、このようにコールバック関数を呼び出すにはどうすればよいですか?
関数の場合callback
は、前に呼び出すだけですanimate
callback();
(selector).animate({styles},speed,easing,callback)
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 を呼び出して、この機能を追加する必要をまったくなくす方がよいでしょう。