adjustCSS.js 1.9 KB

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