aboutsummaryrefslogtreecommitdiffstats
path: root/src/css/adjustCSS.js
diff options
context:
space:
mode:
authorMr21 <thomastortorini@gmail.com>2015-02-04 14:10:14 +0100
committerRichard Gibson <richard.gibson@gmail.com>2015-03-09 12:00:10 -0400
commit9b03f6df88a8d9dbda3f7893cdd84e3a3c70da17 (patch)
tree27eab5fa090fa4bbbe065667dde8d7b5cc94e49e /src/css/adjustCSS.js
parent34da7d552982d8ab7b18c2ceca9786d5023930f6 (diff)
downloadjquery-9b03f6df88a8d9dbda3f7893cdd84e3a3c70da17.tar.gz
jquery-9b03f6df88a8d9dbda3f7893cdd84e3a3c70da17.zip
CSS: Support relative adjustment in any applicable unit
Fixes gh-1711 Closes gh-2011
Diffstat (limited to 'src/css/adjustCSS.js')
-rw-r--r--src/css/adjustCSS.js61
1 files changed, 61 insertions, 0 deletions
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;
+});