diff options
author | Mr21 <thomastortorini@gmail.com> | 2015-02-04 14:10:14 +0100 |
---|---|---|
committer | Richard Gibson <richard.gibson@gmail.com> | 2015-03-09 12:00:10 -0400 |
commit | 9b03f6df88a8d9dbda3f7893cdd84e3a3c70da17 (patch) | |
tree | 27eab5fa090fa4bbbe065667dde8d7b5cc94e49e /src | |
parent | 34da7d552982d8ab7b18c2ceca9786d5023930f6 (diff) | |
download | jquery-9b03f6df88a8d9dbda3f7893cdd84e3a3c70da17.tar.gz jquery-9b03f6df88a8d9dbda3f7893cdd84e3a3c70da17.zip |
CSS: Support relative adjustment in any applicable unit
Fixes gh-1711
Closes gh-2011
Diffstat (limited to 'src')
-rw-r--r-- | src/css.js | 17 | ||||
-rw-r--r-- | src/css/adjustCSS.js | 61 | ||||
-rw-r--r-- | src/effects.js | 56 | ||||
-rw-r--r-- | src/var/rcssNum.js | 7 |
4 files changed, 83 insertions, 58 deletions
diff --git a/src/css.js b/src/css.js index d8e60b857..497b0ec53 100644 --- a/src/css.js +++ b/src/css.js @@ -3,11 +3,13 @@ define([ "./var/pnum", "./core/access", "./css/var/rmargin", + "./var/rcssNum", "./css/var/rnumnonpx", "./css/var/cssExpand", "./css/var/isHidden", "./css/var/getStyles", "./css/curCSS", + "./css/adjustCSS", "./css/defaultDisplay", "./css/addGetHookIf", "./css/support", @@ -17,8 +19,8 @@ define([ "./css/swap", "./core/ready", "./selector" // contains -], function( jQuery, pnum, access, rmargin, rnumnonpx, cssExpand, isHidden, - getStyles, curCSS, defaultDisplay, addGetHookIf, support, dataPriv ) { +], function( jQuery, pnum, access, rmargin, rcssNum, rnumnonpx, cssExpand, isHidden, + getStyles, curCSS, adjustCSS, defaultDisplay, addGetHookIf, support, dataPriv ) { var // Swappable if display is none or starts with table @@ -26,7 +28,6 @@ var // See here for display values: https://developer.mozilla.org/en-US/docs/CSS/display rdisplayswap = /^(none|table(?!-c[ea]).+)/, rnumsplit = new RegExp( "^(" + pnum + ")(.*)$", "i" ), - rrelNum = new RegExp( "^([+-])=(" + pnum + ")", "i" ), cssShow = { position: "absolute", visibility: "hidden", display: "block" }, cssNormalTransform = { @@ -272,8 +273,8 @@ jQuery.extend({ type = typeof value; // Convert "+=" or "-=" to relative numbers (#7345) - if ( type === "string" && (ret = rrelNum.exec( value )) ) { - value = ( ret[1] + 1 ) * ret[2] + parseFloat( jQuery.css( elem, name ) ); + if ( type === "string" && (ret = rcssNum.exec( value )) && ret[ 1 ] ) { + value = adjustCSS( elem, name, ret ); // Fixes bug #9237 type = "number"; } @@ -283,9 +284,9 @@ jQuery.extend({ return; } - // If a number was passed in, add 'px' (except for certain CSS properties) - if ( type === "number" && !jQuery.cssNumber[ origName ] ) { - value += "px"; + // If a number was passed in, add the unit (except for certain CSS properties) + if ( type === "number" ) { + value += ret && ret[ 3 ] || ( jQuery.cssNumber[ origName ] ? "" : "px" ); } // Support: IE9-11+ diff --git a/src/css/adjustCSS.js b/src/css/adjustCSS.js new file mode 100644 index 000000000..05fddd15b --- /dev/null +++ b/src/css/adjustCSS.js @@ -0,0 +1,61 @@ +define([ + "../core", + "../var/rcssNum" +], function( jQuery, rcssNum ) { + +function adjustCSS( elem, prop, valueParts, tween ) { + var adjusted, + scale = 1, + maxIterations = 20, + currentValue = tween ? + function() { return tween.cur(); } : + function() { return jQuery.css( elem, prop, "" ); }, + initial = currentValue(), + unit = valueParts && valueParts[ 3 ] || ( jQuery.cssNumber[ prop ] ? "" : "px" ), + // Starting value computation is required for potential unit mismatches + initialInUnit = ( jQuery.cssNumber[ prop ] || unit !== "px" && +initial ) && + rcssNum.exec( jQuery.css( elem, prop ) ); + + if ( initialInUnit && initialInUnit[ 3 ] !== unit ) { + // Trust units reported by jQuery.css + unit = unit || initialInUnit[ 3 ]; + + // Make sure we update the tween properties later on + valueParts = valueParts || []; + + // Iteratively approximate from a nonzero starting point + initialInUnit = +initial || 1; + + do { + // If previous iteration zeroed out, double until we get *something*. + // Use string for doubling so we don't accidentally see scale as unchanged below + scale = scale || ".5"; + + // Adjust and apply + initialInUnit = initialInUnit / scale; + jQuery.style( elem, prop, initialInUnit + unit ); + + // Update scale, tolerating zero or NaN from tween.cur() + // Break the loop if scale is unchanged or perfect, or if we've just had enough. + } while ( + scale !== (scale = currentValue() / initial) && scale !== 1 && --maxIterations + ); + } + + if ( valueParts ) { + initialInUnit = +initialInUnit || +initial || 0; + // Apply relative offset (+=/-=) if specified + adjusted = valueParts[ 1 ] ? + initialInUnit + ( valueParts[ 1 ] + 1 ) * valueParts[ 2 ] : + +valueParts[ 2 ]; + if ( tween ) { + tween.unit = unit; + tween.start = initialInUnit; + tween.end = adjusted; + } + } + return adjusted; +} + +return adjustCSS; +}); diff --git a/src/effects.js b/src/effects.js index 3aa408b51..e19b04b06 100644 --- a/src/effects.js +++ b/src/effects.js @@ -1,9 +1,10 @@ define([ "./core", "./var/document", - "./var/pnum", + "./var/rcssNum", "./css/var/cssExpand", "./css/var/isHidden", + "./css/adjustCSS", "./css/defaultDisplay", "./data/var/dataPriv", @@ -13,63 +14,18 @@ define([ "./css", "./deferred", "./traversing" -], function( jQuery, document, pnum, cssExpand, isHidden, defaultDisplay, dataPriv ) { +], function( jQuery, document, rcssNum, cssExpand, + isHidden, adjustCSS, defaultDisplay, dataPriv ) { var fxNow, timerId, rfxtypes = /^(?:toggle|show|hide)$/, - rfxnum = new RegExp( "^(?:([+-])=|)(" + pnum + ")([a-z%]*)$", "i" ), rrun = /queueHooks$/, animationPrefilters = [ defaultPrefilter ], tweeners = { "*": [ function( prop, value ) { - var tween = this.createTween( prop, value ), - target = tween.cur(), - parts = rfxnum.exec( value ), - unit = parts && parts[ 3 ] || ( jQuery.cssNumber[ prop ] ? "" : "px" ), - - // Starting value computation is required for potential unit mismatches - start = ( jQuery.cssNumber[ prop ] || unit !== "px" && +target ) && - rfxnum.exec( jQuery.css( tween.elem, prop ) ), - scale = 1, - maxIterations = 20; - - if ( start && start[ 3 ] !== unit ) { - // Trust units reported by jQuery.css - unit = unit || start[ 3 ]; - - // Make sure we update the tween properties later on - parts = parts || []; - - // Iteratively approximate from a nonzero starting point - start = +target || 1; - - do { - // If previous iteration zeroed out, double until we get *something*. - // Use string for doubling so we don't accidentally see scale as unchanged below - scale = scale || ".5"; - - // Adjust and apply - start = start / scale; - jQuery.style( tween.elem, prop, start + unit ); - - // Update scale, tolerating zero or NaN from tween.cur(), - // break the loop if scale is unchanged or perfect, or if we've just had enough - } while ( - scale !== (scale = tween.cur() / target) && scale !== 1 && --maxIterations - ); - } - - // Update tween properties - if ( parts ) { - start = tween.start = +start || +target || 0; - tween.unit = unit; - // If a +=/-= token was provided, we're doing a relative animation - tween.end = parts[ 1 ] ? - start + ( parts[ 1 ] + 1 ) * parts[ 2 ] : - +parts[ 2 ]; - } - + var tween = this.createTween( prop, value ); + adjustCSS( tween.elem, prop, rcssNum.exec( value ), tween ); return tween; } ] }; diff --git a/src/var/rcssNum.js b/src/var/rcssNum.js new file mode 100644 index 000000000..2fc3938a5 --- /dev/null +++ b/src/var/rcssNum.js @@ -0,0 +1,7 @@ +define([ + "../var/pnum" +], function( pnum ) { + +return new RegExp( "^(?:([+-])=|)(" + pnum + ")([a-z%]*)$", "i" ); + +}); |