Massive FX rewrite. Full list of changes:

- You can now animate non-px values (em and %, for example)
- You can animate things things that previously broke (like font-size)
- You can now write fx plugins (to add in the ability to animate colors, for example)
- Advanced queueing controls were added (.queue(), .dequeue(), and {queue:false})
- Step was re-tooled to work with the new fx plugin scheme
- Added the ability to do relative animations
- Made it so that you can do simultaneous animations on a single element
- A complete refactoring of jQuery.fx, improving naming, and usability
This commit is contained in:
John Resig 2007-09-07 21:57:40 +00:00
parent 3abf3125cb
commit 902554a38d

View file

@ -66,10 +66,11 @@ jQuery.fn.extend({
}, },
animate: function( prop, speed, easing, callback ) { animate: function( prop, speed, easing, callback ) {
return this.queue(function(){ var opt = jQuery.speed(speed, easing, callback);
var hidden = jQuery(this).is(":hidden"),
opt = jQuery.speed(speed, easing, callback), return this[ opt.queue === false ? "each" : "queue" ](function(){
self = this; opt = jQuery.extend({}, opt);
var hidden = jQuery(this).is(":hidden"), self = this;
for ( var p in prop ) { for ( var p in prop ) {
if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden ) if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden )
@ -87,14 +88,36 @@ jQuery.fn.extend({
if ( opt.overflow != null ) if ( opt.overflow != null )
this.style.overflow = "hidden"; this.style.overflow = "hidden";
this.curAnim = jQuery.extend({}, prop); opt.curAnim = jQuery.extend({}, prop);
jQuery.each( prop, function(name, val){ jQuery.each( prop, function(name, val){
var e = new jQuery.fx( self, opt, name ); var e = new jQuery.fx( self, opt, name );
if ( val.constructor == Number )
e.custom( e.cur() || 0, val ); if ( /toggle|show|hide/.test(val) )
else
e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop ); e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop );
else {
var parts = val.toString().match(/^([+-]?)([\d.]+)(.*)$/),
start = e.cur(true) || 0;
if ( parts ) {
end = parseFloat(parts[2]),
unit = parts[3] || "px";
// We need to compute starting value
if ( unit != "px" ) {
self.style[ name ] = end + unit;
start = (end / e.cur(true)) * start;
self.style[ name ] = start + unit;
}
// If a +/- token was provided, we're doing a relative animation
if ( parts[1] )
end = ((parts[1] == "-" ? -1 : 1) * end) + start;
e.custom( start, end, unit );
} else
e.custom( start, val, "" );
}
}); });
// For JS strict compliance // For JS strict compliance
@ -102,23 +125,37 @@ jQuery.fn.extend({
}); });
}, },
queue: function(type,fn){ queue: function(type, fn){
if ( !fn ) { if ( !fn ) {
fn = type; fn = type;
type = "fx"; type = "fx";
} }
if ( !arguments.length )
return queue( this[0], type );
return this.each(function(){ return this.each(function(){
if ( !this.queue ) if ( fn.constructor == Array )
this.queue = {}; queue(this, type, fn);
else {
queue(this, type).push( fn );
if ( !this.queue[type] ) if ( queue(this, type).length == 1 )
this.queue[type] = [];
this.queue[type].push( fn );
if ( this.queue[type].length == 1 )
fn.apply(this); fn.apply(this);
}
});
},
dequeue: function(type){
type = type || "fx";
return this.each(function(){
var q = queue(this, type);
q.shift();
if ( q.length )
q[0].apply( this );
}); });
}, },
@ -134,6 +171,22 @@ jQuery.fn.extend({
}); });
function queue( elem, type, array ) {
if ( !elem )
return;
if ( !elem.queue )
elem.queue = {};
if ( !elem.queue[type] )
elem.queue[type] = [];
if ( array )
elem.queue[type] = jQuery.makeArray(array);
return elem.queue[type];
}
jQuery.extend({ jQuery.extend({
speed: function(speed, easing, fn) { speed: function(speed, easing, fn) {
@ -151,7 +204,7 @@ jQuery.extend({
// Queueing // Queueing
opt.old = opt.complete; opt.old = opt.complete;
opt.complete = function(){ opt.complete = function(){
jQuery.dequeue(this, "fx"); jQuery(this).dequeue();
if ( jQuery.isFunction( opt.old ) ) if ( jQuery.isFunction( opt.old ) )
opt.old.apply( this ); opt.old.apply( this );
}; };
@ -168,71 +221,58 @@ jQuery.extend({
} }
}, },
queue: {},
dequeue: function(elem,type){
type = type || "fx";
if ( elem.queue && elem.queue[type] ) {
// Remove self
elem.queue[type].shift();
// Get next function
var f = elem.queue[type][0];
if ( f ) f.apply( elem );
}
},
timers: [], timers: [],
fx: function( elem, options, prop ){ fx: function( elem, options, prop ){
this.options = options;
this.elem = elem;
this.prop = prop;
var z = this, y = elem.style, if ( !options.orig )
isprop = elem[prop] != null && y[prop] == null; options.orig = {};
}
});
jQuery.fx.prototype = {
// Simple function for setting a style value // Simple function for setting a style value
z.a = function(){ update: function(){
if ( options.step ) if ( this.options.step )
options.step.apply( elem, [ z.now ] ); this.options.step.apply( this.elem, [ this.now, this ] );
if ( prop == "opacity" ) (jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );
jQuery.attr(y, "opacity", z.now); // Let attr handle opacity
else {
if ( isprop )
elem[prop] = parseInt(z.now);
else
y[prop] = parseInt(z.now) + "px";
// Set display property to block for height/width animations // Set display property to block for height/width animations
if ( prop == "height" || prop == "width" ) if ( this.prop == "height" || this.prop == "width" )
y.display = "block"; this.elem.style.display = "block";
} },
};
// Figure out the maximum number to run to
z.max = function(){
return parseFloat( jQuery.css(elem,prop) );
};
// Get the current size // Get the current size
z.cur = function(){ cur: function(force){
if ( isprop ) return elem[prop]; if ( this.elem[this.prop] != null && this.elem.style[this.prop] == null )
var r = parseFloat( jQuery.curCSS(elem, prop) ); return this.elem[ this.prop ];
return r && r > -10000 ? r : z.max();
}; var r = parseFloat(jQuery.curCSS(this.elem, this.prop, force));
return r && r > -10000 ? r : parseFloat(jQuery.css(this.elem, this.prop)) || 0;
},
// Start an animation from one number to another // Start an animation from one number to another
z.custom = function(from,to){ custom: function(from, to, unit){
z.startTime = (new Date()).getTime(); this.startTime = (new Date()).getTime();
z.now = from; this.start = from;
z.a(); this.end = to;
this.unit = unit || this.unit || "px";
this.now = this.start;
this.pos = this.state = 0;
this.update();
var self = this;
function t(){ function t(){
return z.step(from, to); return self.step();
} }
t.elem = elem; t.elem = this.elem;
jQuery.timers.push(t); jQuery.timers.push(t);
@ -248,98 +288,110 @@ jQuery.extend({
clearInterval( timer ); clearInterval( timer );
}, 13); }, 13);
} }
}; },
// Simple 'show' function // Simple 'show' function
z.show = function(){ show: function(){
if ( !elem.orig ) elem.orig = {};
// Remember where we started, so that we can go back to it later // Remember where we started, so that we can go back to it later
elem.orig[prop] = jQuery.attr( elem.style, prop ); this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );
this.options.show = true;
options.show = true;
// Begin the animation // Begin the animation
z.custom(0, this.cur()); this.custom(0, this.cur());
// Make sure that we start at a small width/height to avoid any // Make sure that we start at a small width/height to avoid any
// flash of content // flash of content
if ( prop != "opacity" ) if ( this.prop == "width" || this.prop == "height" )
y[prop] = "1px"; this.elem.style[this.prop] = "1px";
// Start by showing the element // Start by showing the element
jQuery(elem).show(); jQuery(this.elem).show();
}; },
// Simple 'hide' function // Simple 'hide' function
z.hide = function(){ hide: function(){
if ( !elem.orig ) elem.orig = {};
// Remember where we started, so that we can go back to it later // Remember where we started, so that we can go back to it later
elem.orig[prop] = jQuery.attr( elem.style, prop ); this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );
this.options.hide = true;
options.hide = true;
// Begin the animation // Begin the animation
z.custom(this.cur(), 0); this.custom(this.cur(), 0);
}; },
// Each step of an animation // Each step of an animation
z.step = function(firstNum, lastNum){ step: function(){
var t = (new Date()).getTime(); var t = (new Date()).getTime();
if (t > options.duration + z.startTime) { if ( t > this.options.duration + this.startTime ) {
z.now = lastNum; this.now = this.end;
z.a(); this.pos = this.state = 1;
this.update();
if (elem.curAnim) elem.curAnim[ prop ] = true; this.options.curAnim[ this.prop ] = true;
var done = true; var done = true;
for ( var i in elem.curAnim ) for ( var i in this.options.curAnim )
if ( elem.curAnim[i] !== true ) if ( this.options.curAnim[i] !== true )
done = false; done = false;
if ( done ) { if ( done ) {
if ( options.display != null ) { if ( this.options.display != null ) {
// Reset the overflow // Reset the overflow
y.overflow = options.overflow; this.elem.style.overflow = this.options.overflow;
// Reset the display // Reset the display
y.display = options.display; this.elem.style.display = this.options.display;
if ( jQuery.css(elem, "display") == "none" ) if ( jQuery.css(this.elem, "display") == "none" )
y.display = "block"; this.elem.style.display = "block";
} }
// Hide the element if the "hide" operation was done // Hide the element if the "hide" operation was done
if ( options.hide ) if ( this.options.hide )
y.display = "none"; this.elem.style.display = "none";
// Reset the properties, if the item has been hidden or shown // Reset the properties, if the item has been hidden or shown
if ( options.hide || options.show ) if ( this.options.hide || this.options.show )
for ( var p in elem.curAnim ) for ( var p in this.options.curAnim )
jQuery.attr(y, p, elem.orig[p]); jQuery.attr(this.elem.style, p, this.options.orig[p]);
} }
// If a callback was provided, execute it // If a callback was provided, execute it
if ( done && jQuery.isFunction( options.complete ) ) if ( done && jQuery.isFunction( this.options.complete ) )
// Execute the complete function // Execute the complete function
options.complete.apply( elem ); this.options.complete.apply( this.elem );
return false; return false;
} else { } else {
var n = t - this.startTime; var n = t - this.startTime;
// Figure out where in the animation we are and set the number this.state = n / this.options.duration;
var p = n / options.duration;
// Perform the easing function, defaults to swing // Perform the easing function, defaults to swing
z.now = jQuery.easing[options.easing || (jQuery.easing.swing ? "swing" : "linear")](p, n, firstNum, (lastNum-firstNum), options.duration); this.pos = jQuery.easing[this.options.easing || (jQuery.easing.swing ? "swing" : "linear")](this.state, n, 0, 1, this.options.duration);
this.now = this.start + ((this.end - this.start) * this.pos);
// Perform the next step of the animation // Perform the next step of the animation
z.a(); this.update();
} }
return true; return true;
};
} }
});
};
jQuery.fx.step = {
scrollLeft: function(fx){
fx.elem.scrollLeft = fx.now;
},
scrollTop: function(fx){
fx.elem.scrollTop = fx.now;
},
opacity: function(fx){
jQuery.attr(fx.elem.style, "opacity", fx.now);
},
_default: function(fx){
fx.elem.style[ fx.prop ] = fx.now + fx.unit;
}
};