aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJohn Resig <jeresig@gmail.com>2011-04-16 09:16:45 -0700
committerJohn Resig <jeresig@gmail.com>2011-04-16 09:16:45 -0700
commit8312e2d852cc9825c321f53694f0f8ababcdefa0 (patch)
treee2732a371ed38fa53693f5aae3779fcb5bbde798 /src
parentce7b311b9227e86d7f03c002cbaf1796f8086059 (diff)
parentbcc8187b059792d9f9c0510e23c08ae037916c7e (diff)
downloadjquery-8312e2d852cc9825c321f53694f0f8ababcdefa0.tar.gz
jquery-8312e2d852cc9825c321f53694f0f8ababcdefa0.zip
Merge branch 'effectsCleanup' of https://github.com/louisremi/jquery into louisremi-effectsCleanup
Diffstat (limited to 'src')
-rw-r--r--src/effects.js164
1 files changed, 84 insertions, 80 deletions
diff --git a/src/effects.js b/src/effects.js
index a43d5cb3c..2c7b44cb0 100644
--- a/src/effects.js
+++ b/src/effects.js
@@ -1,8 +1,7 @@
(function( jQuery ) {
var elemdisplay = {},
- iframe = null,
- iframeDoc = null,
+ iframe, iframeDoc,
rfxtypes = /^(?:toggle|show|hide)$/,
rfxnum = /^([+\-]=)?([\d+.\-]+)([a-z%]*)$/i,
timerId,
@@ -19,15 +18,6 @@ var elemdisplay = {},
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame;
-function clearFxNow() {
- fxNow = undefined;
-}
-
-function createFxNow() {
- setTimeout( clearFxNow, 0 );
- return ( fxNow = jQuery.now() );
-}
-
jQuery.fn.extend({
show: function( speed, easing, callback ) {
var elem, display;
@@ -134,25 +124,32 @@ jQuery.fn.extend({
jQuery._mark( this );
}
- var opt = jQuery.extend({}, optall), p,
+ var opt = jQuery.extend({}, optall),
isElement = this.nodeType === 1,
hidden = isElement && jQuery(this).is(":hidden"),
- self = this;
+ name, val, p,
+ display, e,
+ parts, start, end, unit;
+
+ // will store per property easing and be used to determine when an animation is complete
+ opt.animatedProperties = {};
for ( p in prop ) {
- var name = jQuery.camelCase( p );
+ // property name normalization
+ name = jQuery.camelCase( p );
if ( p !== name ) {
prop[ name ] = prop[ p ];
delete prop[ p ];
- p = name;
}
- if ( prop[p] === "hide" && hidden || prop[p] === "show" && !hidden ) {
+ val = prop[name];
+
+ if ( val === "hide" && hidden || val === "show" && !hidden ) {
return opt.complete.call(this);
}
- if ( isElement && ( p === "height" || p === "width" ) ) {
+ if ( isElement && ( name === "height" || name === "width" ) ) {
// Make sure that nothing sneaks out
// Record all 3 overflow attributes because IE does not
// change the overflow attribute when overflowX and
@@ -183,38 +180,37 @@ jQuery.fn.extend({
}
}
- if ( jQuery.isArray( prop[p] ) ) {
- // Create (if needed) and add to specialEasing
- (opt.specialEasing = opt.specialEasing || {})[p] = prop[p][1];
- prop[p] = prop[p][0];
- }
+ // easing resolution: per property > opt.specialEasing > opt.easing > 'swing' (default)
+ opt.animatedProperties[name] = jQuery.isArray( val ) ?
+ val[1]:
+ opt.specialEasing && opt.specialEasing[name] || opt.easing || 'swing';
}
if ( opt.overflow != null ) {
this.style.overflow = "hidden";
}
- opt.curAnim = jQuery.extend({}, prop);
+ for ( p in prop ) {
+ e = new jQuery.fx( this, opt, p );
- jQuery.each( prop, function( name, val ) {
- var e = new jQuery.fx( self, opt, name );
+ val = prop[p];
if ( rfxtypes.test(val) ) {
- e[ val === "toggle" ? hidden ? "show" : "hide" : val ]( prop );
+ e[ val === "toggle" ? hidden ? "show" : "hide" : val ]();
} else {
- var parts = rfxnum.exec(val),
- start = e.cur();
+ parts = rfxnum.exec(val);
+ start = e.cur();
if ( parts ) {
- var end = parseFloat( parts[2] ),
- unit = parts[3] || ( jQuery.cssNumber[ name ] ? "" : "px" );
+ end = parseFloat( parts[2] );
+ unit = parts[3] || ( jQuery.cssNumber[ name ] ? "" : "px" );
// We need to compute starting value
if ( unit !== "px" ) {
- jQuery.style( self, name, (end || 1) + unit);
+ jQuery.style( this, p, (end || 1) + unit);
start = ((end || 1) / e.cur()) * start;
- jQuery.style( self, name, start + unit);
+ jQuery.style( this, p, start + unit);
}
// If a +=/-= token was provided, we're doing a relative animation
@@ -228,7 +224,7 @@ jQuery.fn.extend({
e.custom( start, val, "" );
}
}
- });
+ }
// For JS strict compliance
return true;
@@ -236,19 +232,19 @@ jQuery.fn.extend({
},
stop: function( clearQueue, gotoEnd ) {
- var timers = jQuery.timers;
-
if ( clearQueue ) {
this.queue([]);
}
this.each(function() {
+ var timers = jQuery.timers,
+ i = timers.length;
// clear marker counters if we know they won't be
if ( !gotoEnd ) {
jQuery._unmark( true, this );
}
// go in reverse order so anything added to the queue during the loop is ignored
- for ( var i = timers.length - 1; i >= 0; i-- ) {
+ while ( i-- ) {
if ( timers[i].elem === this ) {
if (gotoEnd) {
// force the next step to be the last
@@ -270,16 +266,6 @@ jQuery.fn.extend({
});
-function genFx( type, num ) {
- var obj = {};
-
- jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function() {
- obj[ this ] = type;
- });
-
- return obj;
-}
-
// Generate shortcuts for custom animations
jQuery.each({
slideDown: genFx("show", 1),
@@ -339,9 +325,7 @@ jQuery.extend({
this.elem = elem;
this.prop = prop;
- if ( !options.orig ) {
- options.orig = {};
- }
+ options.orig = options.orig || {};
}
});
@@ -435,26 +419,27 @@ jQuery.fx.prototype = {
// Each step of an animation
step: function( gotoEnd ) {
var t = fxNow || createFxNow(),
- done = true;
+ done = true,
+ elem = this.elem,
+ options = this.options,
+ i, n;
- if ( gotoEnd || t >= this.options.duration + this.startTime ) {
+ if ( gotoEnd || t >= options.duration + this.startTime ) {
this.now = this.end;
this.pos = this.state = 1;
this.update();
- this.options.curAnim[ this.prop ] = true;
+ options.animatedProperties[ this.prop ] = true;
- for ( var i in this.options.curAnim ) {
- if ( this.options.curAnim[i] !== true ) {
+ for ( i in options.animatedProperties ) {
+ if ( options.animatedProperties[i] !== true ) {
done = false;
}
}
if ( done ) {
// Reset the overflow
- if ( this.options.overflow != null && !jQuery.support.shrinkWrapBlocks ) {
- var elem = this.elem,
- options = this.options;
+ if ( options.overflow != null && !jQuery.support.shrinkWrapBlocks ) {
jQuery.each( [ "", "X", "Y" ], function (index, value) {
elem.style[ "overflow" + value ] = options.overflow[index];
@@ -462,33 +447,35 @@ jQuery.fx.prototype = {
}
// Hide the element if the "hide" operation was done
- if ( this.options.hide ) {
- jQuery(this.elem).hide();
+ if ( options.hide ) {
+ jQuery(elem).hide();
}
// Reset the properties, if the item has been hidden or shown
- if ( this.options.hide || this.options.show ) {
- for ( var p in this.options.curAnim ) {
- jQuery.style( this.elem, p, this.options.orig[p] );
+ if ( options.hide || options.show ) {
+ for ( var p in options.animatedProperties ) {
+ jQuery.style( elem, p, options.orig[p] );
}
}
// Execute the complete function
- this.options.complete.call( this.elem );
+ options.complete.call( elem );
}
return false;
} else {
- var n = t - this.startTime;
- this.state = n / this.options.duration;
-
- // Perform the easing function, defaults to swing
- var specialEasing = this.options.specialEasing && this.options.specialEasing[this.prop];
- var defaultEasing = this.options.easing || (jQuery.easing.swing ? "swing" : "linear");
- this.pos = jQuery.easing[specialEasing || defaultEasing](this.state, n, 0, 1, this.options.duration);
- this.now = this.start + ((this.end - this.start) * this.pos);
+ // classical easing cannot be used with an Infinity duration
+ if ( options.duration == Infinity ) {
+ this.now = t;
+ } else {
+ n = t - this.startTime;
+ this.state = n / options.duration;
+ // Perform the easing function, defaults to swing
+ this.pos = jQuery.easing[options.animatedProperties[this.prop]](this.state, n, 0, 1, options.duration);
+ this.now = this.start + ((this.end - this.start) * this.pos);
+ }
// Perform the next step of the animation
this.update();
}
@@ -499,11 +486,11 @@ jQuery.fx.prototype = {
jQuery.extend( jQuery.fx, {
tick: function() {
- var timers = jQuery.timers;
-
- for ( var i = 0; i < timers.length; i++ ) {
+ var timers = jQuery.timers,
+ i = timers.length;
+ while ( i-- ) {
if ( !timers[i]() ) {
- timers.splice(i--, 1);
+ timers.splice(i, 1);
}
}
@@ -549,6 +536,7 @@ if ( jQuery.expr && jQuery.expr.filters ) {
};
}
+// Try to restore the default display value of an element
function defaultDisplay( nodeName ) {
if ( !elemdisplay[ nodeName ] ) {
@@ -558,12 +546,9 @@ function defaultDisplay( nodeName ) {
elem.remove();
+ // If the simple way fails,
+ // get element's real default display by attaching it to a temp iframe
if ( display === "none" || display === "" ) {
-
- // Get element's real default display by attaching it to a temp iframe
- // Conritbutions from Louis Remi and Julian Aurbourg
- // based on recommendation by Louis Remi
-
// No iframe to use yet, so create it
if ( !iframe ) {
iframe = document.createElement( "iframe" );
@@ -596,6 +581,25 @@ function defaultDisplay( nodeName ) {
return elemdisplay[ nodeName ];
}
+// Animations created synchronously will run synchronously
+function createFxNow() {
+ setTimeout( clearFxNow, 0 );
+ return ( fxNow = jQuery.now() );
+}
+
+function clearFxNow() {
+ fxNow = undefined;
+}
+
+// Generate parameters to create a standard animation
+function genFx( type, num ) {
+ var obj = {};
+ jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function() {
+ obj[ this ] = type;
+ });
+
+ return obj;
+}
})( jQuery );