css.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. define( [
  2. "./core",
  3. "./var/pnum",
  4. "./core/access",
  5. "./css/var/rmargin",
  6. "./var/document",
  7. "./var/rcssNum",
  8. "./css/var/rnumnonpx",
  9. "./css/var/cssExpand",
  10. "./css/var/getStyles",
  11. "./css/var/swap",
  12. "./css/curCSS",
  13. "./css/adjustCSS",
  14. "./css/addGetHookIf",
  15. "./css/support",
  16. "./core/init",
  17. "./core/ready",
  18. "./selector" // contains
  19. ], function( jQuery, pnum, access, rmargin, document, rcssNum, rnumnonpx, cssExpand,
  20. getStyles, swap, curCSS, adjustCSS, addGetHookIf, support ) {
  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. cssPrefixes = [ "Webkit", "Moz", "ms" ],
  34. emptyStyle = document.createElement( "div" ).style;
  35. // Return a css property mapped to a potentially vendor prefixed property
  36. function vendorPropName( name ) {
  37. // Shortcut for names that are not vendor prefixed
  38. if ( name in emptyStyle ) {
  39. return name;
  40. }
  41. // Check for vendor prefixed names
  42. var capName = name[ 0 ].toUpperCase() + name.slice( 1 ),
  43. i = cssPrefixes.length;
  44. while ( i-- ) {
  45. name = cssPrefixes[ i ] + capName;
  46. if ( name in emptyStyle ) {
  47. return name;
  48. }
  49. }
  50. }
  51. // Return a property mapped along what jQuery.cssProps suggests or to
  52. // a vendor prefixed property.
  53. function finalPropName( name ) {
  54. var ret = jQuery.cssProps[ name ];
  55. if ( !ret ) {
  56. ret = jQuery.cssProps[ name ] = vendorPropName( name ) || name;
  57. }
  58. return ret;
  59. }
  60. function setPositiveNumber( elem, value, subtract ) {
  61. // Any relative (+/-) values have already been
  62. // normalized at this point
  63. var matches = rcssNum.exec( value );
  64. return matches ?
  65. // Guard against undefined "subtract", e.g., when used as in cssHooks
  66. Math.max( 0, matches[ 2 ] - ( subtract || 0 ) ) + ( matches[ 3 ] || "px" ) :
  67. value;
  68. }
  69. function augmentWidthOrHeight( elem, name, extra, isBorderBox, styles ) {
  70. var i,
  71. val = 0;
  72. // If we already have the right measurement, avoid augmentation
  73. if ( extra === ( isBorderBox ? "border" : "content" ) ) {
  74. i = 4;
  75. // Otherwise initialize for horizontal or vertical properties
  76. } else {
  77. i = name === "width" ? 1 : 0;
  78. }
  79. for ( ; i < 4; i += 2 ) {
  80. // Both box models exclude margin, so add it if we want it
  81. if ( extra === "margin" ) {
  82. val += jQuery.css( elem, extra + cssExpand[ i ], true, styles );
  83. }
  84. if ( isBorderBox ) {
  85. // border-box includes padding, so remove it if we want content
  86. if ( extra === "content" ) {
  87. val -= jQuery.css( elem, "padding" + cssExpand[ i ], true, styles );
  88. }
  89. // At this point, extra isn't border nor margin, so remove border
  90. if ( extra !== "margin" ) {
  91. val -= jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles );
  92. }
  93. } else {
  94. // At this point, extra isn't content, so add padding
  95. val += jQuery.css( elem, "padding" + cssExpand[ i ], true, styles );
  96. // At this point, extra isn't content nor padding, so add border
  97. if ( extra !== "padding" ) {
  98. val += jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles );
  99. }
  100. }
  101. }
  102. return val;
  103. }
  104. function getWidthOrHeight( elem, name, extra ) {
  105. // Start with computed style
  106. var valueIsBorderBox,
  107. styles = getStyles( elem ),
  108. val = curCSS( elem, name, styles ),
  109. isBorderBox = jQuery.css( elem, "boxSizing", false, styles ) === "border-box";
  110. // Computed unit is not pixels. Stop here and return.
  111. if ( rnumnonpx.test( val ) ) {
  112. return val;
  113. }
  114. // Check for style in case a browser which returns unreliable values
  115. // for getComputedStyle silently falls back to the reliable elem.style
  116. valueIsBorderBox = isBorderBox &&
  117. ( support.boxSizingReliable() || val === elem.style[ name ] );
  118. // Fall back to offsetWidth/Height when value is "auto"
  119. // This happens for inline elements with no explicit setting (gh-3571)
  120. if ( val === "auto" ) {
  121. val = elem[ "offset" + name[ 0 ].toUpperCase() + name.slice( 1 ) ];
  122. }
  123. // Normalize "", auto, and prepare for extra
  124. val = parseFloat( val ) || 0;
  125. // Use the active box-sizing model to add/subtract irrelevant styles
  126. return ( val +
  127. augmentWidthOrHeight(
  128. elem,
  129. name,
  130. extra || ( isBorderBox ? "border" : "content" ),
  131. valueIsBorderBox,
  132. styles
  133. )
  134. ) + "px";
  135. }
  136. jQuery.extend( {
  137. // Add in style property hooks for overriding the default
  138. // behavior of getting and setting a style property
  139. cssHooks: {
  140. opacity: {
  141. get: function( elem, computed ) {
  142. if ( computed ) {
  143. // We should always get a number back from opacity
  144. var ret = curCSS( elem, "opacity" );
  145. return ret === "" ? "1" : ret;
  146. }
  147. }
  148. }
  149. },
  150. // Don't automatically add "px" to these possibly-unitless properties
  151. cssNumber: {
  152. "animationIterationCount": true,
  153. "columnCount": true,
  154. "fillOpacity": true,
  155. "flexGrow": true,
  156. "flexShrink": true,
  157. "fontWeight": true,
  158. "lineHeight": true,
  159. "opacity": true,
  160. "order": true,
  161. "orphans": true,
  162. "widows": true,
  163. "zIndex": true,
  164. "zoom": true
  165. },
  166. // Add in properties whose names you wish to fix before
  167. // setting or getting the value
  168. cssProps: {
  169. "float": "cssFloat"
  170. },
  171. // Get and set the style property on a DOM Node
  172. style: function( elem, name, value, extra ) {
  173. // Don't set styles on text and comment nodes
  174. if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || !elem.style ) {
  175. return;
  176. }
  177. // Make sure that we're working with the right name
  178. var ret, type, hooks,
  179. origName = jQuery.camelCase( name ),
  180. isCustomProp = rcustomProp.test( name ),
  181. style = elem.style;
  182. // Make sure that we're working with the right name. We don't
  183. // want to query the value if it is a CSS custom property
  184. // since they are user-defined.
  185. if ( !isCustomProp ) {
  186. name = finalPropName( origName );
  187. }
  188. // Gets hook for the prefixed version, then unprefixed version
  189. hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ];
  190. // Check if we're setting a value
  191. if ( value !== undefined ) {
  192. type = typeof value;
  193. // Convert "+=" or "-=" to relative numbers (#7345)
  194. if ( type === "string" && ( ret = rcssNum.exec( value ) ) && ret[ 1 ] ) {
  195. value = adjustCSS( elem, name, ret );
  196. // Fixes bug #9237
  197. type = "number";
  198. }
  199. // Make sure that null and NaN values aren't set (#7116)
  200. if ( value == null || value !== value ) {
  201. return;
  202. }
  203. // If a number was passed in, add the unit (except for certain CSS properties)
  204. if ( type === "number" ) {
  205. value += ret && ret[ 3 ] || ( jQuery.cssNumber[ origName ] ? "" : "px" );
  206. }
  207. // background-* props affect original clone's values
  208. if ( !support.clearCloneStyle && value === "" && name.indexOf( "background" ) === 0 ) {
  209. style[ name ] = "inherit";
  210. }
  211. // If a hook was provided, use that value, otherwise just set the specified value
  212. if ( !hooks || !( "set" in hooks ) ||
  213. ( value = hooks.set( elem, value, extra ) ) !== undefined ) {
  214. if ( isCustomProp ) {
  215. style.setProperty( name, value );
  216. } else {
  217. style[ name ] = value;
  218. }
  219. }
  220. } else {
  221. // If a hook was provided get the non-computed value from there
  222. if ( hooks && "get" in hooks &&
  223. ( ret = hooks.get( elem, false, extra ) ) !== undefined ) {
  224. return ret;
  225. }
  226. // Otherwise just get the value from the style object
  227. return style[ name ];
  228. }
  229. },
  230. css: function( elem, name, extra, styles ) {
  231. var val, num, hooks,
  232. origName = jQuery.camelCase( name ),
  233. isCustomProp = rcustomProp.test( name );
  234. // Make sure that we're working with the right name. We don't
  235. // want to modify the value if it is a CSS custom property
  236. // since they are user-defined.
  237. if ( !isCustomProp ) {
  238. name = finalPropName( origName );
  239. }
  240. // Try prefixed name followed by the unprefixed name
  241. hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ];
  242. // If a hook was provided get the computed value from there
  243. if ( hooks && "get" in hooks ) {
  244. val = hooks.get( elem, true, extra );
  245. }
  246. // Otherwise, if a way to get the computed value exists, use that
  247. if ( val === undefined ) {
  248. val = curCSS( elem, name, styles );
  249. }
  250. // Convert "normal" to computed value
  251. if ( val === "normal" && name in cssNormalTransform ) {
  252. val = cssNormalTransform[ name ];
  253. }
  254. // Make numeric if forced or a qualifier was provided and val looks numeric
  255. if ( extra === "" || extra ) {
  256. num = parseFloat( val );
  257. return extra === true || isFinite( num ) ? num || 0 : val;
  258. }
  259. return val;
  260. }
  261. } );
  262. jQuery.each( [ "height", "width" ], function( i, name ) {
  263. jQuery.cssHooks[ name ] = {
  264. get: function( elem, computed, extra ) {
  265. if ( computed ) {
  266. // Certain elements can have dimension info if we invisibly show them
  267. // but it must have a current display style that would benefit
  268. return rdisplayswap.test( jQuery.css( elem, "display" ) ) &&
  269. // Support: Safari 8+
  270. // Table columns in Safari have non-zero offsetWidth & zero
  271. // getBoundingClientRect().width unless display is changed.
  272. // Support: IE <=11 only
  273. // Running getBoundingClientRect on a disconnected node
  274. // in IE throws an error.
  275. ( !elem.getClientRects().length || !elem.getBoundingClientRect().width ) ?
  276. swap( elem, cssShow, function() {
  277. return getWidthOrHeight( elem, name, extra );
  278. } ) :
  279. getWidthOrHeight( elem, name, extra );
  280. }
  281. },
  282. set: function( elem, value, extra ) {
  283. var matches,
  284. styles = extra && getStyles( elem ),
  285. subtract = extra && augmentWidthOrHeight(
  286. elem,
  287. name,
  288. extra,
  289. jQuery.css( elem, "boxSizing", false, styles ) === "border-box",
  290. styles
  291. );
  292. // Convert to pixels if value adjustment is needed
  293. if ( subtract && ( matches = rcssNum.exec( value ) ) &&
  294. ( matches[ 3 ] || "px" ) !== "px" ) {
  295. elem.style[ name ] = value;
  296. value = jQuery.css( elem, name );
  297. }
  298. return setPositiveNumber( elem, value, subtract );
  299. }
  300. };
  301. } );
  302. jQuery.cssHooks.marginLeft = addGetHookIf( support.reliableMarginLeft,
  303. function( elem, computed ) {
  304. if ( computed ) {
  305. return ( parseFloat( curCSS( elem, "marginLeft" ) ) ||
  306. elem.getBoundingClientRect().left -
  307. swap( elem, { marginLeft: 0 }, function() {
  308. return elem.getBoundingClientRect().left;
  309. } )
  310. ) + "px";
  311. }
  312. }
  313. );
  314. // These hooks are used by animate to expand properties
  315. jQuery.each( {
  316. margin: "",
  317. padding: "",
  318. border: "Width"
  319. }, function( prefix, suffix ) {
  320. jQuery.cssHooks[ prefix + suffix ] = {
  321. expand: function( value ) {
  322. var i = 0,
  323. expanded = {},
  324. // Assumes a single number if not a string
  325. parts = typeof value === "string" ? value.split( " " ) : [ value ];
  326. for ( ; i < 4; i++ ) {
  327. expanded[ prefix + cssExpand[ i ] + suffix ] =
  328. parts[ i ] || parts[ i - 2 ] || parts[ 0 ];
  329. }
  330. return expanded;
  331. }
  332. };
  333. if ( !rmargin.test( prefix ) ) {
  334. jQuery.cssHooks[ prefix + suffix ].set = setPositiveNumber;
  335. }
  336. } );
  337. jQuery.fn.extend( {
  338. css: function( name, value ) {
  339. return access( this, function( elem, name, value ) {
  340. var styles, len,
  341. map = {},
  342. i = 0;
  343. if ( Array.isArray( name ) ) {
  344. styles = getStyles( elem );
  345. len = name.length;
  346. for ( ; i < len; i++ ) {
  347. map[ name[ i ] ] = jQuery.css( elem, name[ i ], false, styles );
  348. }
  349. return map;
  350. }
  351. return value !== undefined ?
  352. jQuery.style( elem, name, value ) :
  353. jQuery.css( elem, name );
  354. }, name, value, arguments.length > 1 );
  355. }
  356. } );
  357. return jQuery;
  358. } );