css.js 11 KB

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