diff options
author | Mike Sherov <mike.sherov@gmail.com> | 2011-12-08 20:01:23 -0500 |
---|---|---|
committer | Dave Methvin <dave.methvin@gmail.com> | 2011-12-08 20:01:23 -0500 |
commit | 8f5f1b2e6c0f7b8b6d66bfc06c7b169a9443c2b8 (patch) | |
tree | fd5d393097bd7ded122c65e877c5157e7b6d421d /src/css.js | |
parent | 7f6a991313380b74d5fb18782fb6b99fd6c4a22d (diff) | |
download | jquery-8f5f1b2e6c0f7b8b6d66bfc06c7b169a9443c2b8.tar.gz jquery-8f5f1b2e6c0f7b8b6d66bfc06c7b169a9443c2b8.zip |
Fix #8498. Add cssHooks[prop].expand for use by animate().
Diffstat (limited to 'src/css.js')
-rw-r--r-- | src/css.js | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/src/css.js b/src/css.js index 7d5abe975..4d4e2dc82 100644 --- a/src/css.js +++ b/src/css.js @@ -10,8 +10,10 @@ var ralpha = /alpha\([^)]*\)/i, rmargin = /^margin/, cssShow = { position: "absolute", visibility: "hidden", display: "block" }, - cssWidth = [ "Left", "Right" ], - cssHeight = [ "Top", "Bottom" ], + + // order is important! + cssExpand = [ "Top", "Right", "Bottom", "Left" ], + curCSS, getComputedStyle, @@ -169,10 +171,10 @@ jQuery.each(["height", "width"], function( i, name ) { get: function( elem, computed, extra ) { if ( computed ) { if ( elem.offsetWidth !== 0 ) { - return getWH( elem, name, extra ); + return getWidthOrHeight( elem, name, extra ); } else { return jQuery.swap( elem, cssShow, function() { - return getWH( elem, name, extra ); + return getWidthOrHeight( elem, name, extra ); }); } } @@ -326,24 +328,23 @@ if ( document.documentElement.currentStyle ) { curCSS = getComputedStyle || currentStyle; -function getWH( elem, name, extra ) { +function getWidthOrHeight( elem, name, extra ) { // Start with offset property var val = name === "width" ? elem.offsetWidth : elem.offsetHeight, - which = name === "width" ? cssWidth : cssHeight, - i = 0, - len = which.length; + i = name === "width" ? 1 : 0, + len = 4; if ( val > 0 ) { if ( extra !== "border" ) { - for ( ; i < len; i++ ) { + for ( ; i < len; i += 2 ) { if ( !extra ) { - val -= parseFloat( jQuery.css( elem, "padding" + which[ i ] ) ) || 0; + val -= parseFloat( jQuery.css( elem, "padding" + cssExpand[ i ] ) ) || 0; } if ( extra === "margin" ) { - val += parseFloat( jQuery.css( elem, extra + which[ i ] ) ) || 0; + val += parseFloat( jQuery.css( elem, extra + cssExpand[ i ] ) ) || 0; } else { - val -= parseFloat( jQuery.css( elem, "border" + which[ i ] + "Width" ) ) || 0; + val -= parseFloat( jQuery.css( elem, "border" + cssExpand[ i ] + "Width" ) ) || 0; } } } @@ -354,20 +355,20 @@ function getWH( elem, name, extra ) { // Fall back to computed then uncomputed css if necessary val = curCSS( elem, name, name ); if ( val < 0 || val == null ) { - val = elem.style[ name ] || 0; + val = elem.style[ name ]; } // Normalize "", auto, and prepare for extra val = parseFloat( val ) || 0; // Add padding, border, margin if ( extra ) { - for ( ; i < len; i++ ) { - val += parseFloat( jQuery.css( elem, "padding" + which[ i ] ) ) || 0; + for ( ; i < len; i += 2 ) { + val += parseFloat( jQuery.css( elem, "padding" + cssExpand[ i ] ) ) || 0; if ( extra !== "padding" ) { - val += parseFloat( jQuery.css( elem, "border" + which[ i ] + "Width" ) ) || 0; + val += parseFloat( jQuery.css( elem, "border" + cssExpand[ i ] + "Width" ) ) || 0; } if ( extra === "margin" ) { - val += parseFloat( jQuery.css( elem, extra + which[ i ] ) ) || 0; + val += parseFloat( jQuery.css( elem, extra + cssExpand[ i ]) ) || 0; } } } @@ -388,4 +389,29 @@ if ( jQuery.expr && jQuery.expr.filters ) { }; } +// These hooks are used by animate to expand properties +jQuery.each({ + margin: "", + padding: "", + border: "Width" +}, function( prefix, suffix ) { + + jQuery.cssHooks[ prefix + suffix ] = { + expand: function( value ) { + var i, + + // assumes a single number if not a string + parts = typeof value === "string" ? value.split(" ") : [ value ], + expanded = {}; + + for ( i = 0; i < 4; i++ ) { + expanded[ prefix + cssExpand[ i ] + suffix ] = + parts[ i ] || parts[ i - 2 ] || parts[ 0 ]; + } + + return expanded; + } + }; +}); + })( jQuery ); |