Made hide, show, toggle, slideUp, slideDown, and slideToggle animate margins and paddings in addition to height, width, and opacity (results in a much-smoother animation).

This commit is contained in:
John Resig 2009-01-05 01:14:36 +00:00
parent 19a3ea2c7f
commit cc66e6affe

View file

@ -1,11 +1,20 @@
var elemdisplay = {}; var elemdisplay = {},
fxHeight = [ "height", "marginTop", "marginBottom", "paddingTop", "paddingBottom" ],
fxWidth = [ "width", "marginLeft", "marginRight", "paddingLeft", "paddingRight" ],
fxOpacity = [ "opacity" ];
function genFx( type, width, height, extra ){
var obj = {};
jQuery.each(width.concat( height || [], extra || []), function(){
obj[ this ] = type;
});
return obj;
}
jQuery.fn.extend({ jQuery.fn.extend({
show: function(speed,callback){ show: function(speed,callback){
if ( speed ) { if ( speed ) {
return this.animate({ return this.animate( genFx("show", fxHeight, fxWidth, fxOpacity), speed, callback);
height: "show", width: "show", opacity: "show"
}, speed, callback);
} else { } else {
for ( var i = 0, l = this.length; i < l; i++ ){ for ( var i = 0, l = this.length; i < l; i++ ){
var old = jQuery.data(this[i], "olddisplay"); var old = jQuery.data(this[i], "olddisplay");
@ -39,9 +48,7 @@ jQuery.fn.extend({
hide: function(speed,callback){ hide: function(speed,callback){
if ( speed ) { if ( speed ) {
return this.animate({ return this.animate( genFx("hide", fxHeight, fxWidth, fxOpacity), speed, callback);
height: "hide", width: "hide", opacity: "hide"
}, speed, callback);
} else { } else {
for ( var i = 0, l = this.length; i < l; i++ ){ for ( var i = 0, l = this.length; i < l; i++ ){
var old = jQuery.data(this[i], "olddisplay"); var old = jQuery.data(this[i], "olddisplay");
@ -66,9 +73,7 @@ jQuery.fn.extend({
var state = bool ? fn : jQuery(this).is(":hidden"); var state = bool ? fn : jQuery(this).is(":hidden");
jQuery(this)[ state ? "show" : "hide" ](); jQuery(this)[ state ? "show" : "hide" ]();
}) : }) :
this.animate({ this.animate(genFx("toggle", fxHeight, fxWidth, fxOpacity), fn, fn2);
height: "toggle", width: "toggle", opacity: "toggle"
}, fn, fn2);
}, },
fadeTo: function(speed,to,callback){ fadeTo: function(speed,to,callback){
@ -165,9 +170,9 @@ jQuery.fn.extend({
// Generate shortcuts for custom animations // Generate shortcuts for custom animations
jQuery.each({ jQuery.each({
slideDown: { height:"show" }, slideDown: genFx("show", fxHeight),
slideUp: { height: "hide" }, slideUp: genFx("hide", fxHeight),
slideToggle: { height: "toggle" }, slideToggle: genFx("toggle", fxHeight),
fadeIn: { opacity: "show" }, fadeIn: { opacity: "show" },
fadeOut: { opacity: "hide" } fadeOut: { opacity: "hide" }
}, function( name, props ){ }, function( name, props ){