adjustCSS.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. define( [
  2. "../core",
  3. "../var/rcssNum"
  4. ], function( jQuery, rcssNum ) {
  5. "use strict";
  6. function adjustCSS( elem, prop, valueParts, tween ) {
  7. var adjusted, scale,
  8. maxIterations = 20,
  9. currentValue = tween ?
  10. function() {
  11. return tween.cur();
  12. } :
  13. function() {
  14. return jQuery.css( elem, prop, "" );
  15. },
  16. initial = currentValue(),
  17. unit = valueParts && valueParts[ 3 ] || ( jQuery.cssNumber[ prop ] ? "" : "px" ),
  18. // Starting value computation is required for potential unit mismatches
  19. initialInUnit = elem.nodeType &&
  20. ( jQuery.cssNumber[ prop ] || unit !== "px" && +initial ) &&
  21. rcssNum.exec( jQuery.css( elem, prop ) );
  22. if ( initialInUnit && initialInUnit[ 3 ] !== unit ) {
  23. // Support: Firefox <=54
  24. // Halve the iteration target value to prevent interference from CSS upper bounds (gh-2144)
  25. initial = initial / 2;
  26. // Trust units reported by jQuery.css
  27. unit = unit || initialInUnit[ 3 ];
  28. // Iteratively approximate from a nonzero starting point
  29. initialInUnit = +initial || 1;
  30. while ( maxIterations-- ) {
  31. // Evaluate and update our best guess (doubling guesses that zero out).
  32. // Finish if the scale equals or crosses 1 (making the old*new product non-positive).
  33. jQuery.style( elem, prop, initialInUnit + unit );
  34. if ( ( 1 - scale ) * ( 1 - ( scale = currentValue() / initial || 0.5 ) ) <= 0 ) {
  35. maxIterations = 0;
  36. }
  37. initialInUnit = initialInUnit / scale;
  38. }
  39. initialInUnit = initialInUnit * 2;
  40. jQuery.style( elem, prop, initialInUnit + unit );
  41. // Make sure we update the tween properties later on
  42. valueParts = valueParts || [];
  43. }
  44. if ( valueParts ) {
  45. initialInUnit = +initialInUnit || +initial || 0;
  46. // Apply relative offset (+=/-=) if specified
  47. adjusted = valueParts[ 1 ] ?
  48. initialInUnit + ( valueParts[ 1 ] + 1 ) * valueParts[ 2 ] :
  49. +valueParts[ 2 ];
  50. if ( tween ) {
  51. tween.unit = unit;
  52. tween.start = initialInUnit;
  53. tween.end = adjusted;
  54. }
  55. }
  56. return adjusted;
  57. }
  58. return adjustCSS;
  59. } );