css.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. define( [
  2. "./core",
  3. "./core/access",
  4. "./core/camelCase",
  5. "./core/nodeName",
  6. "./var/rcssNum",
  7. "./css/var/rnumnonpx",
  8. "./css/var/cssExpand",
  9. "./css/var/getStyles",
  10. "./css/var/swap",
  11. "./css/curCSS",
  12. "./css/adjustCSS",
  13. "./css/addGetHookIf",
  14. "./css/support",
  15. "./css/finalPropName",
  16. "./core/init",
  17. "./core/ready",
  18. "./selector" // contains
  19. ], function( jQuery, access, camelCase, nodeName, rcssNum, rnumnonpx, cssExpand,
  20. getStyles, swap, curCSS, adjustCSS, addGetHookIf, support, finalPropName ) {
  21. "use strict";
  22. var
  23. // Swappable if display is none or starts with table
  24. // except "table", "table-cell", or "table-caption"
  25. // See here for display values: https://developer.mozilla.org/en-US/docs/CSS/display
  26. rdisplayswap = /^(none|table(?!-c[ea]).+)/,
  27. rcustomProp = /^--/,
  28. cssShow = { position: "absolute", visibility: "hidden", display: "block" },
  29. cssNormalTransform = {
  30. letterSpacing: "0",
  31. fontWeight: "400"
  32. };
  33. function setPositiveNumber( _elem, value, subtract ) {
  34. // Any relative (+/-) values have already been
  35. // normalized at this point
  36. var matches = rcssNum.exec( value );
  37. return matches ?
  38. // Guard against undefined "subtract", e.g., when used as in cssHooks
  39. Math.max( 0, matches[ 2 ] - ( subtract || 0 ) ) + ( matches[ 3 ] || "px" ) :
  40. value;
  41. }
  42. function boxModelAdjustment( elem, dimension, box, isBorderBox, styles, computedVal ) {
  43. var i = dimension === "width" ? 1 : 0,
  44. extra = 0,
  45. delta = 0;
  46. // Adjustment may not be necessary
  47. if ( box === ( isBorderBox ? "border" : "content" ) ) {
  48. return 0;
  49. }
  50. for ( ; i < 4; i += 2 ) {
  51. // Both box models exclude margin
  52. if ( box === "margin" ) {
  53. delta += jQuery.css( elem, box + cssExpand[ i ], true, styles );
  54. }
  55. // If we get here with a content-box, we're seeking "padding" or "border" or "margin"
  56. if ( !isBorderBox ) {
  57. // Add padding
  58. delta += jQuery.css( elem, "padding" + cssExpand[ i ], true, styles );
  59. // For "border" or "margin", add border
  60. if ( box !== "padding" ) {
  61. delta += jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles );
  62. // But still keep track of it otherwise
  63. } else {
  64. extra += jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles );
  65. }
  66. // If we get here with a border-box (content + padding + border), we're seeking "content" or
  67. // "padding" or "margin"
  68. } else {
  69. // For "content", subtract padding
  70. if ( box === "content" ) {
  71. delta -= jQuery.css( elem, "padding" + cssExpand[ i ], true, styles );
  72. }
  73. // For "content" or "padding", subtract border
  74. if ( box !== "margin" ) {
  75. delta -= jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles );
  76. }
  77. }
  78. }
  79. // Account for positive content-box scroll gutter when requested by providing computedVal
  80. if ( !isBorderBox && computedVal >= 0 ) {
  81. // offsetWidth/offsetHeight is a rounded sum of content, padding, scroll gutter, and border
  82. // Assuming integer scroll gutter, subtract the rest and round down
  83. delta += Math.max( 0, Math.ceil(
  84. elem[ "offset" + dimension[ 0 ].toUpperCase() + dimension.slice( 1 ) ] -
  85. computedVal -
  86. delta -
  87. extra -
  88. 0.5
  89. // If offsetWidth/offsetHeight is unknown, then we can't determine content-box scroll gutter
  90. // Use an explicit zero to avoid NaN (gh-3964)
  91. ) ) || 0;
  92. }
  93. return delta;
  94. }
  95. function getWidthOrHeight( elem, dimension, extra ) {
  96. // Start with computed style
  97. var styles = getStyles( elem ),
  98. // To avoid forcing a reflow, only fetch boxSizing if we need it (gh-4322).
  99. // Fake content-box until we know it's needed to know the true value.
  100. boxSizingNeeded = !support.boxSizingReliable() || extra,
  101. isBorderBox = boxSizingNeeded &&
  102. jQuery.css( elem, "boxSizing", false, styles ) === "border-box",
  103. valueIsBorderBox = isBorderBox,
  104. val = curCSS( elem, dimension, styles ),
  105. offsetProp = "offset" + dimension[ 0 ].toUpperCase() + dimension.slice( 1 );
  106. // Support: Firefox <=54
  107. // Return a confounding non-pixel value or feign ignorance, as appropriate.
  108. if ( rnumnonpx.test( val ) ) {
  109. if ( !extra ) {
  110. return val;
  111. }
  112. val = "auto";
  113. }
  114. // Support: IE 9 - 11 only
  115. // Use offsetWidth/offsetHeight for when box sizing is unreliable.
  116. // In those cases, the computed value can be trusted to be border-box.
  117. if ( ( !support.boxSizingReliable() && isBorderBox ||
  118. // Support: IE 10 - 11+, Edge 15 - 18+
  119. // IE/Edge misreport `getComputedStyle` of table rows with width/height
  120. // set in CSS while `offset*` properties report correct values.
  121. // Interestingly, in some cases IE 9 doesn't suffer from this issue.
  122. !support.reliableTrDimensions() && nodeName( elem, "tr" ) ||
  123. // Fall back to offsetWidth/offsetHeight when value is "auto"
  124. // This happens for inline elements with no explicit setting (gh-3571)
  125. val === "auto" ||
  126. // Support: Android <=4.1 - 4.3 only
  127. // Also use offsetWidth/offsetHeight for misreported inline dimensions (gh-3602)
  128. !parseFloat( val ) && jQuery.css( elem, "display", false, styles ) === "inline" ) &&
  129. // Make sure the element is visible & connected
  130. elem.getClientRects().length ) {
  131. isBorderBox = jQuery.css( elem, "boxSizing", false, styles ) === "border-box";
  132. // Where available, offsetWidth/offsetHeight approximate border box dimensions.
  133. // Where not available (e.g., SVG), assume unreliable box-sizing and interpret the
  134. // retrieved value as a content box dimension.
  135. valueIsBorderBox = offsetProp in elem;
  136. if ( valueIsBorderBox ) {
  137. val = elem[ offsetProp ];
  138. }
  139. }
  140. // Normalize "" and auto
  141. val = parseFloat( val ) || 0;
  142. // Adjust for the element's box model
  143. return ( val +
  144. boxModelAdjustment(
  145. elem,
  146. dimension,
  147. extra || ( isBorderBox ? "border" : "content" ),
  148. valueIsBorderBox,
  149. styles,
  150. // Provide the current computed size to request scroll gutter calculation (gh-3589)
  151. val
  152. )
  153. ) + "px";
  154. }
  155. jQuery.extend( {
  156. // Add in style property hooks for overriding the default
  157. // behavior of getting and setting a style property
  158. cssHooks: {
  159. opacity: {
  160. get: function( elem, computed ) {
  161. if ( computed ) {
  162. // We should always get a number back from opacity
  163. var ret = curCSS( elem, "opacity" );
  164. return ret === "" ? "1" : ret;
  165. }
  166. }
  167. }
  168. },
  169. // Don't automatically add "px" to these possibly-unitless properties
  170. cssNumber: {
  171. "animationIterationCount": true,
  172. "columnCount": true,
  173. "fillOpacity": true,
  174. "flexGrow": true,
  175. "flexShrink": true,
  176. "fontWeight": true,
  177. "gridArea": true,
  178. "gridColumn": true,
  179. "gridColumnEnd": true,
  180. "gridColumnStart": true,
  181. "gridRow": true,
  182. "gridRowEnd": true,
  183. "gridRowStart": true,
  184. "lineHeight": true,
  185. "opacity": true,
  186. "order": true,
  187. "orphans": true,
  188. "widows": true,
  189. "zIndex": true,
  190. "zoom": true
  191. },
  192. // Add in properties whose names you wish to fix before
  193. // setting or getting the value
  194. cssProps: {},
  195. // Get and set the style property on a DOM Node
  196. style: function( elem, name, value, extra ) {
  197. // Don't set styles on text and comment nodes
  198. if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || !elem.style ) {
  199. return;
  200. }
  201. // Make sure that we're working with the right name
  202. var ret, type, hooks,
  203. origName = camelCase( name ),
  204. isCustomProp = rcustomProp.test( name ),
  205. style = elem.style;
  206. // Make sure that we're working with the right name. We don't
  207. // want to query the value if it is a CSS custom property
  208. // since they are user-defined.
  209. if ( !isCustomProp ) {
  210. name = finalPropName( origName );
  211. }
  212. // Gets hook for the prefixed version, then unprefixed version
  213. hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ];
  214. // Check if we're setting a value
  215. if ( value !== undefined ) {
  216. type = typeof value;
  217. // Convert "+=" or "-=" to relative numbers (#7345)
  218. if ( type === "string" && ( ret = rcssNum.exec( value ) ) && ret[ 1 ] ) {
  219. value = adjustCSS( elem, name, ret );
  220. // Fixes bug #9237
  221. type = "number";
  222. }
  223. // Make sure that null and NaN values aren't set (#7116)
  224. if ( value == null || value !== value ) {
  225. return;
  226. }
  227. // If a number was passed in, add the unit (except for certain CSS properties)
  228. // The isCustomProp check can be removed in jQuery 4.0 when we only auto-append
  229. // "px" to a few hardcoded values.
  230. if ( type === "number" && !isCustomProp ) {
  231. value += ret && ret[ 3 ] || ( jQuery.cssNumber[ origName ] ? "" : "px" );
  232. }
  233. // background-* props affect original clone's values
  234. if ( !support.clearCloneStyle && value === "" && name.indexOf( "background" ) === 0 ) {
  235. style[ name ] = "inherit";
  236. }
  237. // If a hook was provided, use that value, otherwise just set the specified value
  238. if ( !hooks || !( "set" in hooks ) ||
  239. ( value = hooks.set( elem, value, extra ) ) !== undefined ) {
  240. if ( isCustomProp ) {
  241. style.setProperty( name, value );
  242. } else {
  243. style[ name ] = value;
  244. }
  245. }
  246. } else {
  247. // If a hook was provided get the non-computed value from there
  248. if ( hooks && "get" in hooks &&
  249. ( ret = hooks.get( elem, false, extra ) ) !== undefined ) {
  250. return ret;
  251. }
  252. // Otherwise just get the value from the style object
  253. return style[ name ];
  254. }
  255. },
  256. css: function( elem, name, extra, styles ) {
  257. var val, num, hooks,
  258. origName = camelCase( name ),
  259. isCustomProp = rcustomProp.test( name );
  260. // Make sure that we're working with the right name. We don't
  261. // want to modify the value if it is a CSS custom property
  262. // since they are user-defined.
  263. if ( !isCustomProp ) {
  264. name = finalPropName( origName );
  265. }
  266. // Try prefixed name followed by the unprefixed name
  267. hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ];
  268. // If a hook was provided get the computed value from there
  269. if ( hooks && "get" in hooks ) {
  270. val = hooks.get( elem, true, extra );
  271. }
  272. // Otherwise, if a way to get the computed value exists, use that
  273. if ( val === undefined ) {
  274. val = curCSS( elem, name, styles );
  275. }
  276. // Convert "normal" to computed value
  277. if ( val === "normal" && name in cssNormalTransform ) {
  278. val = cssNormalTransform[ name ];
  279. }
  280. // Make numeric if forced or a qualifier was provided and val looks numeric
  281. if ( extra === "" || extra ) {
  282. num = parseFloat( val );
  283. return extra === true || isFinite( num ) ? num || 0 : val;
  284. }
  285. return val;
  286. }
  287. } );
  288. jQuery.each( [ "height", "width" ], function( _i, dimension ) {
  289. jQuery.cssHooks[ dimension ] = {
  290. get: function( elem, computed, extra ) {
  291. if ( computed ) {
  292. // Certain elements can have dimension info if we invisibly show them
  293. // but it must have a current display style that would benefit
  294. return rdisplayswap.test( jQuery.css( elem, "display" ) ) &&
  295. // Support: Safari 8+
  296. // Table columns in Safari have non-zero offsetWidth & zero
  297. // getBoundingClientRect().width unless display is changed.
  298. // Support: IE <=11 only
  299. // Running getBoundingClientRect on a disconnected node
  300. // in IE throws an error.
  301. ( !elem.getClientRects().length || !elem.getBoundingClientRect().width ) ?
  302. swap( elem, cssShow, function() {
  303. return getWidthOrHeight( elem, dimension, extra );
  304. } ) :
  305. getWidthOrHeight( elem, dimension, extra );
  306. }
  307. },
  308. set: function( elem, value, extra ) {
  309. var matches,
  310. styles = getStyles( elem ),
  311. // Only read styles.position if the test has a chance to fail
  312. // to avoid forcing a reflow.
  313. scrollboxSizeBuggy = !support.scrollboxSize() &&
  314. styles.position === "absolute",
  315. // To avoid forcing a reflow, only fetch boxSizing if we need it (gh-3991)
  316. boxSizingNeeded = scrollboxSizeBuggy || extra,
  317. isBorderBox = boxSizingNeeded &&
  318. jQuery.css( elem, "boxSizing", false, styles ) === "border-box",
  319. subtract = extra ?
  320. boxModelAdjustment(
  321. elem,
  322. dimension,
  323. extra,
  324. isBorderBox,
  325. styles
  326. ) :
  327. 0;
  328. // Account for unreliable border-box dimensions by comparing offset* to computed and
  329. // faking a content-box to get border and padding (gh-3699)
  330. if ( isBorderBox && scrollboxSizeBuggy ) {
  331. subtract -= Math.ceil(
  332. elem[ "offset" + dimension[ 0 ].toUpperCase() + dimension.slice( 1 ) ] -
  333. parseFloat( styles[ dimension ] ) -
  334. boxModelAdjustment( elem, dimension, "border", false, styles ) -
  335. 0.5
  336. );
  337. }
  338. // Convert to pixels if value adjustment is needed
  339. if ( subtract && ( matches = rcssNum.exec( value ) ) &&
  340. ( matches[ 3 ] || "px" ) !== "px" ) {
  341. elem.style[ dimension ] = value;
  342. value = jQuery.css( elem, dimension );
  343. }
  344. return setPositiveNumber( elem, value, subtract );
  345. }
  346. };
  347. } );
  348. jQuery.cssHooks.marginLeft = addGetHookIf( support.reliableMarginLeft,
  349. function( elem, computed ) {
  350. if ( computed ) {
  351. return ( parseFloat( curCSS( elem, "marginLeft" ) ) ||
  352. elem.getBoundingClientRect().left -
  353. swap( elem, { marginLeft: 0 }, function() {
  354. return elem.getBoundingClientRect().left;
  355. } )
  356. ) + "px";
  357. }
  358. }
  359. );
  360. // These hooks are used by animate to expand properties
  361. jQuery.each( {
  362. margin: "",
  363. padding: "",
  364. border: "Width"
  365. }, function( prefix, suffix ) {
  366. jQuery.cssHooks[ prefix + suffix ] = {
  367. expand: function( value ) {
  368. var i = 0,
  369. expanded = {},
  370. // Assumes a single number if not a string
  371. parts = typeof value === "string" ? value.split( " " ) : [ value ];
  372. for ( ; i < 4; i++ ) {
  373. expanded[ prefix + cssExpand[ i ] + suffix ] =
  374. parts[ i ] || parts[ i - 2 ] || parts[ 0 ];
  375. }
  376. return expanded;
  377. }
  378. };
  379. if ( prefix !== "margin" ) {
  380. jQuery.cssHooks[ prefix + suffix ].set = setPositiveNumber;
  381. }
  382. } );
  383. jQuery.fn.extend( {
  384. css: function( name, value ) {
  385. return access( this, function( elem, name, value ) {
  386. var styles, len,
  387. map = {},
  388. i = 0;
  389. if ( Array.isArray( name ) ) {
  390. styles = getStyles( elem );
  391. len = name.length;
  392. for ( ; i < len; i++ ) {
  393. map[ name[ i ] ] = jQuery.css( elem, name[ i ], false, styles );
  394. }
  395. return map;
  396. }
  397. return value !== undefined ?
  398. jQuery.style( elem, name, value ) :
  399. jQuery.css( elem, name );
  400. }, name, value, arguments.length > 1 );
  401. }
  402. } );
  403. return jQuery;
  404. } );