adjustCSS.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. define( [
  2. "../core",
  3. "../var/rcssNum"
  4. ], function( jQuery, rcssNum ) {
  5. "use strict";
  6. function adjustCSS( elem, prop, valueParts, tween ) {
  7. var adjusted,
  8. scale = 1,
  9. maxIterations = 20,
  10. currentValue = tween ?
  11. function() { return tween.cur(); } :
  12. function() { return jQuery.css( elem, prop, "" ); },
  13. initial = currentValue(),
  14. unit = valueParts && valueParts[ 3 ] || ( jQuery.cssNumber[ prop ] ? "" : "px" ),
  15. // Starting value computation is required for potential unit mismatches
  16. initialInUnit = ( jQuery.cssNumber[ prop ] || unit !== "px" && +initial ) &&
  17. rcssNum.exec( jQuery.css( elem, prop ) );
  18. if ( initialInUnit && initialInUnit[ 3 ] !== unit ) {
  19. // Trust units reported by jQuery.css
  20. unit = unit || initialInUnit[ 3 ];
  21. // Make sure we update the tween properties later on
  22. valueParts = valueParts || [];
  23. // Iteratively approximate from a nonzero starting point
  24. initialInUnit = +initial || 1;
  25. do {
  26. // If previous iteration zeroed out, double until we get *something*.
  27. // Use string for doubling so we don't accidentally see scale as unchanged below
  28. scale = scale || ".5";
  29. // Adjust and apply
  30. initialInUnit = initialInUnit / scale;
  31. jQuery.style( elem, prop, initialInUnit + unit );
  32. // Update scale, tolerating zero or NaN from tween.cur()
  33. // Break the loop if scale is unchanged or perfect, or if we've just had enough.
  34. } while (
  35. scale !== ( scale = currentValue() / initial ) && scale !== 1 && --maxIterations
  36. );
  37. }
  38. if ( valueParts ) {
  39. initialInUnit = +initialInUnit || +initial || 0;
  40. // Apply relative offset (+=/-=) if specified
  41. adjusted = valueParts[ 1 ] ?
  42. initialInUnit + ( valueParts[ 1 ] + 1 ) * valueParts[ 2 ] :
  43. +valueParts[ 2 ];
  44. if ( tween ) {
  45. tween.unit = unit;
  46. tween.start = initialInUnit;
  47. tween.end = adjusted;
  48. }
  49. }
  50. return adjusted;
  51. }
  52. return adjustCSS;
  53. } );