Skip to content

Commit 2dcf723

Browse files
committed
effects.core: Rework animateClass to support a 'children' option - Fixes #3939 - Option to animate children elements in animateClass
1 parent c241275 commit 2dcf723

File tree

1 file changed

+49
-19
lines changed

1 file changed

+49
-19
lines changed

ui/jquery.effects.core.js

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -225,42 +225,72 @@ $.effects.animateClass = function( value, duration, easing, callback ) {
225225
var animated = $( this ),
226226
baseClass = animated.attr( "class" ),
227227
finalClass,
228-
originalStyleAttr = animated.attr( "style" ) || " ",
229-
originalStyle = getElementStyles.call( this ),
230-
newStyle,
231-
diff,
232-
prop;
228+
allAnimations = o.children ? animated.find( "*" ).andSelf() : animated;
229+
230+
// map the animated objects to store the original styles.
231+
allAnimations = allAnimations.map(function() {
232+
var el = $( this );
233+
return {
234+
el: el,
235+
originalStyleAttr: el.attr( "style" ) || " ",
236+
start: getElementStyles.call( this )
237+
};
238+
});
233239

240+
// apply class change
234241
$.each( classAnimationActions, function(i, action) {
235242
if ( value[ action ] ) {
236243
animated[ action + "Class" ]( value[ action ] );
237244
}
238245
});
239-
newStyle = getElementStyles.call( this );
240246
finalClass = animated.attr( "class" );
247+
248+
// map all animated objects again - calculate new styles and diff
249+
allAnimations = allAnimations.map(function() {
250+
this.end = getElementStyles.call( this.el[ 0 ] );
251+
this.diff = styleDifference( this.start, this.end );
252+
return this;
253+
});
254+
255+
// apply original class
241256
animated.attr( "class", baseClass );
242257

243-
diff = styleDifference( originalStyle, newStyle );
244-
animated
245-
.animate( diff, {
258+
// map all animated objects again - this time collecting a promise
259+
allAnimations = allAnimations.map(function() {
260+
var styleInfo = this,
261+
dfd = $.Deferred();
262+
263+
this.el.animate( this.diff, {
246264
duration: duration,
247265
easing: o.easing,
248266
queue: false,
249267
complete: function() {
250-
animated.attr( "class", finalClass );
268+
dfd.resolve( styleInfo );
269+
}
270+
});
271+
return dfd.promise();
272+
});
251273

252-
if ( typeof animated.attr( "style" ) === "object" ) {
253-
animated.attr( "style" ).cssText = "";
254-
animated.attr( "style" ).cssText = originalStyleAttr;
255-
} else {
256-
animated.attr( "style", originalStyleAttr );
257-
}
274+
// once all animations have completed:
275+
$.when.apply( $, allAnimations.get() ).done(function() {
258276

259-
// this is guarnteed to be there if you use jQuery.speed()
260-
// it also handles dequeuing the next anim...
261-
o.complete.call( this );
277+
// set the final class
278+
animated.attr( "class", finalClass );
279+
280+
// for each animated element
281+
$.each( arguments, function() {
282+
if ( typeof this.el.attr( "style" ) === "object" ) {
283+
this.el.attr( "style" ).cssText = "";
284+
this.el.attr( "style" ).cssText = this.originalStyleAttr;
285+
} else {
286+
this.el.attr( "style", this.originalStyleAttr );
262287
}
263288
});
289+
290+
// this is guarnteed to be there if you use jQuery.speed()
291+
// it also handles dequeuing the next anim...
292+
o.complete.call( animated[ 0 ] );
293+
});
264294
});
265295
};
266296

0 commit comments

Comments
 (0)