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
было бы лучше, чтобы запустить этот код, а затем вызвать живой от него, как только он является полным, устраняя необходимость добавления этой возможности вообще.
'callback(); $ (selector) .animate ({styles}, speed, easing, callback); ', не так ли? – Cherniv
внутри функции анимации. –
В принципе, это не обратный вызов, если вы его вызываете, прежде чем что-то начнется, не так ли? – aletzo