Merge branch 'effectsCleanup' of https://github.com/louisremi/jquery into louisremi-effectsCleanup

This commit is contained in:
John Resig 2011-04-16 09:16:45 -07:00
commit 8312e2d852

156
src/effects.js vendored
View File

@ -1,8 +1,7 @@
(function( jQuery ) { (function( jQuery ) {
var elemdisplay = {}, var elemdisplay = {},
iframe = null, iframe, iframeDoc,
iframeDoc = null,
rfxtypes = /^(?:toggle|show|hide)$/, rfxtypes = /^(?:toggle|show|hide)$/,
rfxnum = /^([+\-]=)?([\d+.\-]+)([a-z%]*)$/i, rfxnum = /^([+\-]=)?([\d+.\-]+)([a-z%]*)$/i,
timerId, timerId,
@ -19,15 +18,6 @@ var elemdisplay = {},
window.mozRequestAnimationFrame || window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame; window.oRequestAnimationFrame;
function clearFxNow() {
fxNow = undefined;
}
function createFxNow() {
setTimeout( clearFxNow, 0 );
return ( fxNow = jQuery.now() );
}
jQuery.fn.extend({ jQuery.fn.extend({
show: function( speed, easing, callback ) { show: function( speed, easing, callback ) {
var elem, display; var elem, display;
@ -134,25 +124,32 @@ jQuery.fn.extend({
jQuery._mark( this ); jQuery._mark( this );
} }
var opt = jQuery.extend({}, optall), p, var opt = jQuery.extend({}, optall),
isElement = this.nodeType === 1, isElement = this.nodeType === 1,
hidden = isElement && jQuery(this).is(":hidden"), 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 ) { for ( p in prop ) {
var name = jQuery.camelCase( p );
// property name normalization
name = jQuery.camelCase( p );
if ( p !== name ) { if ( p !== name ) {
prop[ name ] = prop[ p ]; prop[ name ] = prop[ p ];
delete 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); return opt.complete.call(this);
} }
if ( isElement && ( p === "height" || p === "width" ) ) { if ( isElement && ( name === "height" || name === "width" ) ) {
// Make sure that nothing sneaks out // Make sure that nothing sneaks out
// Record all 3 overflow attributes because IE does not // Record all 3 overflow attributes because IE does not
// change the overflow attribute when overflowX and // change the overflow attribute when overflowX and
@ -183,38 +180,37 @@ jQuery.fn.extend({
} }
} }
if ( jQuery.isArray( prop[p] ) ) { // easing resolution: per property > opt.specialEasing > opt.easing > 'swing' (default)
// Create (if needed) and add to specialEasing opt.animatedProperties[name] = jQuery.isArray( val ) ?
(opt.specialEasing = opt.specialEasing || {})[p] = prop[p][1]; val[1]:
prop[p] = prop[p][0]; opt.specialEasing && opt.specialEasing[name] || opt.easing || 'swing';
}
} }
if ( opt.overflow != null ) { if ( opt.overflow != null ) {
this.style.overflow = "hidden"; 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 ) { val = prop[p];
var e = new jQuery.fx( self, opt, name );
if ( rfxtypes.test(val) ) { if ( rfxtypes.test(val) ) {
e[ val === "toggle" ? hidden ? "show" : "hide" : val ]( prop ); e[ val === "toggle" ? hidden ? "show" : "hide" : val ]();
} else { } else {
var parts = rfxnum.exec(val), parts = rfxnum.exec(val);
start = e.cur(); start = e.cur();
if ( parts ) { if ( parts ) {
var end = parseFloat( parts[2] ), end = parseFloat( parts[2] );
unit = parts[3] || ( jQuery.cssNumber[ name ] ? "" : "px" ); unit = parts[3] || ( jQuery.cssNumber[ name ] ? "" : "px" );
// We need to compute starting value // We need to compute starting value
if ( unit !== "px" ) { if ( unit !== "px" ) {
jQuery.style( self, name, (end || 1) + unit); jQuery.style( this, p, (end || 1) + unit);
start = ((end || 1) / e.cur()) * start; 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 // If a +=/-= token was provided, we're doing a relative animation
@ -228,7 +224,7 @@ jQuery.fn.extend({
e.custom( start, val, "" ); e.custom( start, val, "" );
} }
} }
}); }
// For JS strict compliance // For JS strict compliance
return true; return true;
@ -236,19 +232,19 @@ jQuery.fn.extend({
}, },
stop: function( clearQueue, gotoEnd ) { stop: function( clearQueue, gotoEnd ) {
var timers = jQuery.timers;
if ( clearQueue ) { if ( clearQueue ) {
this.queue([]); this.queue([]);
} }
this.each(function() { this.each(function() {
var timers = jQuery.timers,
i = timers.length;
// clear marker counters if we know they won't be // clear marker counters if we know they won't be
if ( !gotoEnd ) { if ( !gotoEnd ) {
jQuery._unmark( true, this ); jQuery._unmark( true, this );
} }
// go in reverse order so anything added to the queue during the loop is ignored // 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 ( timers[i].elem === this ) {
if (gotoEnd) { if (gotoEnd) {
// force the next step to be the last // 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 // Generate shortcuts for custom animations
jQuery.each({ jQuery.each({
slideDown: genFx("show", 1), slideDown: genFx("show", 1),
@ -339,9 +325,7 @@ jQuery.extend({
this.elem = elem; this.elem = elem;
this.prop = prop; this.prop = prop;
if ( !options.orig ) { options.orig = options.orig || {};
options.orig = {};
}
} }
}); });
@ -435,26 +419,27 @@ jQuery.fx.prototype = {
// Each step of an animation // Each step of an animation
step: function( gotoEnd ) { step: function( gotoEnd ) {
var t = fxNow || createFxNow(), 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.now = this.end;
this.pos = this.state = 1; this.pos = this.state = 1;
this.update(); this.update();
this.options.curAnim[ this.prop ] = true; options.animatedProperties[ this.prop ] = true;
for ( var i in this.options.curAnim ) { for ( i in options.animatedProperties ) {
if ( this.options.curAnim[i] !== true ) { if ( options.animatedProperties[i] !== true ) {
done = false; done = false;
} }
} }
if ( done ) { if ( done ) {
// Reset the overflow // Reset the overflow
if ( this.options.overflow != null && !jQuery.support.shrinkWrapBlocks ) { if ( options.overflow != null && !jQuery.support.shrinkWrapBlocks ) {
var elem = this.elem,
options = this.options;
jQuery.each( [ "", "X", "Y" ], function (index, value) { jQuery.each( [ "", "X", "Y" ], function (index, value) {
elem.style[ "overflow" + value ] = options.overflow[index]; elem.style[ "overflow" + value ] = options.overflow[index];
@ -462,33 +447,35 @@ jQuery.fx.prototype = {
} }
// Hide the element if the "hide" operation was done // Hide the element if the "hide" operation was done
if ( this.options.hide ) { if ( options.hide ) {
jQuery(this.elem).hide(); jQuery(elem).hide();
} }
// Reset the properties, if the item has been hidden or shown // Reset the properties, if the item has been hidden or shown
if ( this.options.hide || this.options.show ) { if ( options.hide || options.show ) {
for ( var p in this.options.curAnim ) { for ( var p in options.animatedProperties ) {
jQuery.style( this.elem, p, this.options.orig[p] ); jQuery.style( elem, p, options.orig[p] );
} }
} }
// Execute the complete function // Execute the complete function
this.options.complete.call( this.elem ); options.complete.call( elem );
} }
return false; return false;
} else { } else {
var n = t - this.startTime; // classical easing cannot be used with an Infinity duration
this.state = n / this.options.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 // Perform the easing function, defaults to swing
var specialEasing = this.options.specialEasing && this.options.specialEasing[this.prop]; this.pos = jQuery.easing[options.animatedProperties[this.prop]](this.state, n, 0, 1, options.duration);
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); this.now = this.start + ((this.end - this.start) * this.pos);
}
// Perform the next step of the animation // Perform the next step of the animation
this.update(); this.update();
} }
@ -499,11 +486,11 @@ jQuery.fx.prototype = {
jQuery.extend( jQuery.fx, { jQuery.extend( jQuery.fx, {
tick: function() { tick: function() {
var timers = jQuery.timers; var timers = jQuery.timers,
i = timers.length;
for ( var i = 0; i < timers.length; i++ ) { while ( i-- ) {
if ( !timers[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 ) { function defaultDisplay( nodeName ) {
if ( !elemdisplay[ nodeName ] ) { if ( !elemdisplay[ nodeName ] ) {
@ -558,12 +546,9 @@ function defaultDisplay( nodeName ) {
elem.remove(); elem.remove();
// If the simple way fails,
// get element's real default display by attaching it to a temp iframe
if ( display === "none" || display === "" ) { 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 // No iframe to use yet, so create it
if ( !iframe ) { if ( !iframe ) {
iframe = document.createElement( "iframe" ); iframe = document.createElement( "iframe" );
@ -596,6 +581,25 @@ function defaultDisplay( nodeName ) {
return elemdisplay[ 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 ); })( jQuery );