No Description

css.js 14KB

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