Skip to content

Commit fa0a33a

Browse files
timmywiltimmywil
timmywil
authored andcommitted
Style edits, code reductions, and optimizations for the effects module
1 parent 28b470d commit fa0a33a

File tree

2 files changed

+55
-54
lines changed

2 files changed

+55
-54
lines changed

src/effects.js

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ jQuery.fn.extend({
120120
},
121121

122122
animate: function( prop, speed, easing, callback ) {
123-
var optall = jQuery.speed(speed, easing, callback);
123+
var optall = jQuery.speed( speed, easing, callback );
124124

125125
if ( jQuery.isEmptyObject( prop ) ) {
126126
return this.each( optall.complete, [ false ] );
@@ -140,8 +140,7 @@ jQuery.fn.extend({
140140
var opt = jQuery.extend( {}, optall ),
141141
isElement = this.nodeType === 1,
142142
hidden = isElement && jQuery(this).is(":hidden"),
143-
name, val, p,
144-
display, e,
143+
name, val, p, e,
145144
parts, start, end, unit;
146145

147146
// will store per property easing and be used to determine when an animation is complete
@@ -182,21 +181,14 @@ jQuery.fn.extend({
182181
// animated
183182
if ( jQuery.css( this, "display" ) === "inline" &&
184183
jQuery.css( this, "float" ) === "none" ) {
185-
if ( !jQuery.support.inlineBlockNeedsLayout ) {
184+
185+
// inline-level elements accept inline-block;
186+
// block-level elements need to be inline with layout
187+
if ( !jQuery.support.inlineBlockNeedsLayout || defaultDisplay( this.nodeName ) === "inline" ) {
186188
this.style.display = "inline-block";
187189

188190
} else {
189-
display = defaultDisplay( this.nodeName );
190-
191-
// inline-level elements accept inline-block;
192-
// block-level elements need to be inline with layout
193-
if ( display === "inline" ) {
194-
this.style.display = "inline-block";
195-
196-
} else {
197-
this.style.display = "inline";
198-
this.style.zoom = 1;
199-
}
191+
this.style.zoom = 1;
200192
}
201193
}
202194
}
@@ -210,7 +202,7 @@ jQuery.fn.extend({
210202
e = new jQuery.fx( this, opt, p );
211203
val = prop[ p ];
212204

213-
if ( rfxtypes.test(val) ) {
205+
if ( rfxtypes.test( val ) ) {
214206
e[ val === "toggle" ? hidden ? "show" : "hide" : val ]();
215207

216208
} else {
@@ -259,10 +251,10 @@ jQuery.fn.extend({
259251
jQuery._unmark( true, this );
260252
}
261253
while ( i-- ) {
262-
if ( timers[i].elem === this ) {
263-
if (gotoEnd) {
254+
if ( timers[ i ].elem === this ) {
255+
if ( gotoEnd ) {
264256
// force the next step to be the last
265-
timers[i](true);
257+
timers[ i ]( true );
266258
}
267259

268260
timers.splice(i, 1);
@@ -294,7 +286,7 @@ function clearFxNow() {
294286
function genFx( type, num ) {
295287
var obj = {};
296288

297-
jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function() {
289+
jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice( 0, num )), function() {
298290
obj[ this ] = type;
299291
});
300292

@@ -303,9 +295,9 @@ function genFx( type, num ) {
303295

304296
// Generate shortcuts for custom animations
305297
jQuery.each({
306-
slideDown: genFx("show", 1),
307-
slideUp: genFx("hide", 1),
308-
slideToggle: genFx("toggle", 1),
298+
slideDown: genFx( "show", 1 ),
299+
slideUp: genFx( "hide", 1 ),
300+
slideToggle: genFx( "toggle", 1 ),
309301
fadeIn: { opacity: "show" },
310302
fadeOut: { opacity: "hide" },
311303
fadeToggle: { opacity: "toggle" }
@@ -317,15 +309,15 @@ jQuery.each({
317309

318310
jQuery.extend({
319311
speed: function( speed, easing, fn ) {
320-
var opt = speed && typeof speed === "object" ? jQuery.extend({}, speed) : {
312+
var opt = speed && typeof speed === "object" ? jQuery.extend( {}, speed ) : {
321313
complete: fn || !fn && easing ||
322314
jQuery.isFunction( speed ) && speed,
323315
duration: speed,
324-
easing: fn && easing || easing && !jQuery.isFunction(easing) && easing
316+
easing: fn && easing || easing && !jQuery.isFunction( easing ) && easing
325317
};
326318

327319
opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
328-
opt.duration in jQuery.fx.speeds ? jQuery.fx.speeds[opt.duration] : jQuery.fx.speeds._default;
320+
opt.duration in jQuery.fx.speeds ? jQuery.fx.speeds[ opt.duration ] : jQuery.fx.speeds._default;
329321

330322
// Queueing
331323
opt.old = opt.complete;
@@ -372,12 +364,12 @@ jQuery.fx.prototype = {
372364
this.options.step.call( this.elem, this.now, this );
373365
}
374366

375-
(jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );
367+
(jQuery.fx.step[ this.prop ] || jQuery.fx.step._default)( this );
376368
},
377369

378370
// Get the current size
379371
cur: function() {
380-
if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
372+
if ( this.elem[ this.prop ] != null && (!this.elem.style || this.elem.style[ this.prop ] == null) ) {
381373
return this.elem[ this.prop ];
382374
}
383375

@@ -396,14 +388,13 @@ jQuery.fx.prototype = {
396388
raf;
397389

398390
this.startTime = fxNow || createFxNow();
399-
this.start = from;
400391
this.end = to;
401-
this.unit = unit || this.unit || ( jQuery.cssNumber[ this.prop ] ? "" : "px" );
402-
this.now = this.start;
392+
this.now = this.start = from;
403393
this.pos = this.state = 0;
394+
this.unit = unit || this.unit || ( jQuery.cssNumber[ this.prop ] ? "" : "px" );
404395

405396
function t( gotoEnd ) {
406-
return self.step(gotoEnd);
397+
return self.step( gotoEnd );
407398
}
408399

409400
t.elem = this.elem;
@@ -429,13 +420,13 @@ jQuery.fx.prototype = {
429420
// Simple 'show' function
430421
show: function() {
431422
// Remember where we started, so that we can go back to it later
432-
this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
423+
this.options.orig[ this.prop ] = jQuery.style( this.elem, this.prop );
433424
this.options.show = true;
434425

435426
// Begin the animation
436427
// Make sure that we start at a small width/height to avoid any
437428
// flash of content
438-
this.custom(this.prop === "width" || this.prop === "height" ? 1 : 0, this.cur());
429+
this.custom( this.prop === "width" || this.prop === "height" ? 1 : 0, this.cur() );
439430

440431
// Start by showing the element
441432
jQuery( this.elem ).show();
@@ -444,11 +435,11 @@ jQuery.fx.prototype = {
444435
// Simple 'hide' function
445436
hide: function() {
446437
// Remember where we started, so that we can go back to it later
447-
this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
438+
this.options.orig[ this.prop ] = jQuery.style( this.elem, this.prop );
448439
this.options.hide = true;
449440

450441
// Begin the animation
451-
this.custom(this.cur(), 0);
442+
this.custom( this.cur(), 0 );
452443
},
453444

454445
// Each step of an animation
@@ -467,7 +458,7 @@ jQuery.fx.prototype = {
467458
options.animatedProperties[ this.prop ] = true;
468459

469460
for ( i in options.animatedProperties ) {
470-
if ( options.animatedProperties[i] !== true ) {
461+
if ( options.animatedProperties[ i ] !== true ) {
471462
done = false;
472463
}
473464
}
@@ -476,14 +467,14 @@ jQuery.fx.prototype = {
476467
// Reset the overflow
477468
if ( options.overflow != null && !jQuery.support.shrinkWrapBlocks ) {
478469

479-
jQuery.each( [ "", "X", "Y" ], function (index, value) {
480-
elem.style[ "overflow" + value ] = options.overflow[index];
470+
jQuery.each( [ "", "X", "Y" ], function( index, value ) {
471+
elem.style[ "overflow" + value ] = options.overflow[ index ];
481472
});
482473
}
483474

484475
// Hide the element if the "hide" operation was done
485476
if ( options.hide ) {
486-
jQuery(elem).hide();
477+
jQuery( elem ).hide();
487478
}
488479

489480
// Reset the properties, if the item has been hidden or shown
@@ -508,8 +499,8 @@ jQuery.fx.prototype = {
508499
this.state = n / options.duration;
509500

510501
// Perform the easing function, defaults to swing
511-
this.pos = jQuery.easing[ options.animatedProperties[ this.prop ] ]( this.state, n, 0, 1, options.duration );
512-
this.now = this.start + ((this.end - this.start) * this.pos);
502+
this.pos = jQuery.easing[ options.animatedProperties[this.prop] ]( this.state, n, 0, 1, options.duration );
503+
this.now = this.start + ( (this.end - this.start) * this.pos );
513504
}
514505
// Perform the next step of the animation
515506
this.update();
@@ -522,7 +513,7 @@ jQuery.fx.prototype = {
522513
jQuery.extend( jQuery.fx, {
523514
tick: function() {
524515
for ( var timers = jQuery.timers, i = 0 ; i < timers.length ; ++i ) {
525-
if ( !timers[i]() ) {
516+
if ( !timers[ i ]() ) {
526517
timers.splice(i--, 1);
527518
}
528519
}
@@ -553,14 +544,22 @@ jQuery.extend( jQuery.fx, {
553544

554545
_default: function( fx ) {
555546
if ( fx.elem.style && fx.elem.style[ fx.prop ] != null ) {
556-
fx.elem.style[ fx.prop ] = (fx.prop === "width" || fx.prop === "height" ? Math.max(0, fx.now) : fx.now) + fx.unit;
547+
fx.elem.style[ fx.prop ] = fx.now + fx.unit;
557548
} else {
558549
fx.elem[ fx.prop ] = fx.now;
559550
}
560551
}
561552
}
562553
});
563554

555+
// Adds width/height step functions
556+
// Do not set anything below 0
557+
jQuery.each([ "width", "height" ], function( i, prop ) {
558+
jQuery.fx.step[ prop ] = function( fx ) {
559+
jQuery.style( fx.elem, prop, Math.max(0, fx.now) );
560+
};
561+
});
562+
564563
if ( jQuery.expr && jQuery.expr.filters ) {
565564
jQuery.expr.filters.animated = function( elem ) {
566565
return jQuery.grep(jQuery.timers, function( fn ) {

src/queue.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ function handleQueueMarkDefer( elem, type, src ) {
44
var deferDataKey = type + "defer",
55
queueDataKey = type + "queue",
66
markDataKey = type + "mark",
7-
defer = jQuery.data( elem, deferDataKey, undefined, true );
7+
defer = jQuery._data( elem, deferDataKey );
88
if ( defer &&
9-
( src === "queue" || !jQuery.data( elem, queueDataKey, undefined, true ) ) &&
10-
( src === "mark" || !jQuery.data( elem, markDataKey, undefined, true ) ) ) {
9+
( src === "queue" || !jQuery._data(elem, queueDataKey) ) &&
10+
( src === "mark" || !jQuery._data(elem, markDataKey) ) ) {
1111
// Give room for hard-coded callbacks to fire first
1212
// and eventually mark/queue something else on the element
1313
setTimeout( function() {
14-
if ( !jQuery.data( elem, queueDataKey, undefined, true ) &&
15-
!jQuery.data( elem, markDataKey, undefined, true ) ) {
14+
if ( !jQuery._data( elem, queueDataKey ) &&
15+
!jQuery._data( elem, markDataKey ) ) {
1616
jQuery.removeData( elem, deferDataKey, true );
1717
defer.resolve();
1818
}
@@ -25,7 +25,7 @@ jQuery.extend({
2525
_mark: function( elem, type ) {
2626
if ( elem ) {
2727
type = (type || "fx") + "mark";
28-
jQuery.data( elem, type, (jQuery.data(elem,type,undefined,true) || 0) + 1, true );
28+
jQuery._data( elem, type, (jQuery._data( elem, type ) || 0) + 1 );
2929
}
3030
},
3131

@@ -38,9 +38,9 @@ jQuery.extend({
3838
if ( elem ) {
3939
type = type || "fx";
4040
var key = type + "mark",
41-
count = force ? 0 : ( (jQuery.data( elem, key, undefined, true) || 1 ) - 1 );
41+
count = force ? 0 : ( (jQuery._data( elem, key ) || 1) - 1 );
4242
if ( count ) {
43-
jQuery.data( elem, key, count, true );
43+
jQuery._data( elem, key, count );
4444
} else {
4545
jQuery.removeData( elem, key, true );
4646
handleQueueMarkDefer( elem, type, "mark" );
@@ -49,13 +49,15 @@ jQuery.extend({
4949
},
5050

5151
queue: function( elem, type, data ) {
52+
var q;
5253
if ( elem ) {
5354
type = (type || "fx") + "queue";
54-
var q = jQuery.data( elem, type, undefined, true );
55+
q = jQuery._data( elem, type );
56+
5557
// Speed up dequeue by getting out quickly if this is just a lookup
5658
if ( data ) {
5759
if ( !q || jQuery.isArray(data) ) {
58-
q = jQuery.data( elem, type, jQuery.makeArray(data), true );
60+
q = jQuery._data( elem, type, jQuery.makeArray(data) );
5961
} else {
6062
q.push( data );
6163
}

0 commit comments

Comments
 (0)