3 // exclude the following css properties to add px
4 var rexclude = /z-?index|font-?weight|opacity|zoom|line-?height/i,
5 ralpha = /alpha\([^)]*\)/,
6 ropacity = /opacity=([^)]*)/,
8 rdashAlpha = /-([a-z])/ig,
10 rnumpx = /^-?\d+(?:px)?$/i,
13 cssShow = { position: "absolute", visibility: "hidden", display:"block" },
14 cssWidth = [ "Left", "Right" ],
15 cssHeight = [ "Top", "Bottom" ],
17 // cache check for defaultView.getComputedStyle
18 getComputedStyle = document.defaultView && document.defaultView.getComputedStyle,
19 // normalize float css property
20 styleFloat = jQuery.support.cssFloat ? "cssFloat" : "styleFloat",
21 fcamelCase = function( all, letter ) {
22 return letter.toUpperCase();
25 jQuery.fn.css = function( name, value ) {
26 return jQuery.access( this, name, value, true, function( elem, name, value ) {
27 if ( value === undefined ) {
28 return jQuery.curCSS( elem, name );
31 if ( typeof value === "number" && !rexclude.test(name) ) {
35 jQuery.style( elem, name, value );
40 style: function( elem, name, value ) {
41 // don't set styles on text and comment nodes
42 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
46 // ignore negative width and height values #1599
47 if ( (name === "width" || name === "height") && parseFloat(value) < 0 ) {
51 var style = elem.style || elem, set = value !== undefined;
53 // IE uses filters for opacity
54 if ( !jQuery.support.opacity && name === "opacity" && style.filter ) {
56 // IE has trouble with opacity if it does not have layout
57 // Force it by setting the zoom level
60 // Set the alpha filter to set the opacity
61 var opacity = parseInt( value, 10 ) + "" === "NaN" ? "" : "alpha(opacity=" + value * 100 + ")";
62 var filter = style.filter || jQuery.curCSS( elem, "filter" ) || "";
63 style.filter = ralpha.test(filter) ? filter.replace(ralpha, opacity) : opacity;
66 return style.filter && style.filter.indexOf("opacity=") >= 0 ?
67 (parseFloat( ropacity.exec(style.filter)[1] ) / 100) + "":
71 // Make sure we're using the right name for getting the float value
72 if ( rfloat.test( name ) ) {
76 name = name.replace(rdashAlpha, fcamelCase);
79 style[ name ] = value;
85 css: function( elem, name, force, extra ) {
86 if ( name === "width" || name === "height" ) {
87 if ( elem.offsetWidth !== 0 ) {
88 val = getWH( elem, name, extra );
91 jQuery.swap( elem, cssShow, function() {
92 val = getWH( elem, name, extra );
96 return Math.max(0, Math.round(val));
99 return jQuery.curCSS( elem, name, force );
102 curCSS: function( elem, name, force ) {
103 var ret, style = elem.style, filter;
105 // IE uses filters for opacity
106 if ( !jQuery.support.opacity && name === "opacity" && elem.currentStyle ) {
107 ret = ropacity.test(elem.currentStyle.filter || "") ?
108 (parseFloat(RegExp.$1) / 100) + "" :
116 // Make sure we're using the right name for getting the float value
117 if ( rfloat.test( name ) ) {
121 if ( !force && style && style[ name ] ) {
124 } else if ( getComputedStyle ) {
126 // Only "float" is needed here
127 if ( rfloat.test( name ) ) {
131 name = name.replace( rupper, "-$1" ).toLowerCase();
133 var defaultView = elem.ownerDocument.defaultView;
135 if ( !defaultView ) {
139 var computedStyle = defaultView.getComputedStyle( elem, null );
141 if ( computedStyle ) {
142 ret = computedStyle.getPropertyValue( name );
145 // We should always get a number back from opacity
146 if ( name === "opacity" && ret === "" ) {
150 } else if ( elem.currentStyle ) {
151 var camelCase = name.replace(rdashAlpha, fcamelCase);
153 ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
155 // From the awesome hack by Dean Edwards
156 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
158 // If we're not dealing with a regular pixel number
159 // but a number that has a weird ending, we need to convert it to pixels
160 if ( !rnumpx.test( ret ) && rnum.test( ret ) ) {
161 // Remember the original values
162 var left = style.left, rsLeft = elem.runtimeStyle.left;
164 // Put in the new values to get a computed value out
165 elem.runtimeStyle.left = elem.currentStyle.left;
166 style.left = camelCase === "fontSize" ? "1em" : (ret || 0);
167 ret = style.pixelLeft + "px";
169 // Revert the changed values
171 elem.runtimeStyle.left = rsLeft;
178 // A method for quickly swapping in/out CSS properties to get correct calculations
179 swap: function( elem, options, callback ) {
182 // Remember the old values, and insert the new ones
183 for ( var name in options ) {
184 old[ name ] = elem.style[ name ];
185 elem.style[ name ] = options[ name ];
188 callback.call( elem );
190 // Revert the old values
191 for ( name in options ) {
192 elem.style[ name ] = old[ name ];
197 function getWH( elem, name, extra ) {
198 var which = name === "width" ? cssWidth : cssHeight,
199 val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
201 if ( extra === "border" ) {
205 jQuery.each( which, function() {
207 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
210 if ( extra === "margin" ) {
211 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
214 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
221 if ( jQuery.expr && jQuery.expr.filters ) {
222 jQuery.expr.filters.hidden = function( elem ) {
223 var width = elem.offsetWidth, height = elem.offsetHeight,
224 skip = elem.nodeName.toLowerCase() === "tr";
226 return width === 0 && height === 0 && !skip ?
228 width > 0 && height > 0 && !skip ?
230 jQuery.curCSS(elem, "display") === "none";
233 jQuery.expr.filters.visible = function( elem ) {
234 return !jQuery.expr.filters.hidden( elem );