1 // exclude the following css properties to add px
2 var rexclude = /z-?index|font-?weight|opacity|zoom|line-?height/i,
3 ralpha = /alpha\([^)]*\)/,
4 ropacity = /opacity=([^)]*)/,
6 rdashAlpha = /-([a-z])/ig,
8 rnumpx = /^-?\d+(?:px)?$/i,
11 cssShow = { position: "absolute", visibility: "hidden", display:"block" },
12 cssWidth = [ "Left", "Right" ],
13 cssHeight = [ "Top", "Bottom" ],
15 // cache check for defaultView.getComputedStyle
16 getComputedStyle = document.defaultView && document.defaultView.getComputedStyle,
17 // normalize float css property
18 styleFloat = jQuery.support.cssFloat ? "cssFloat" : "styleFloat",
19 fcamelCase = function( all, letter ) {
20 return letter.toUpperCase();
23 jQuery.fn.css = function( name, value ) {
24 return access( this, name, value, true, function( elem, name, value ) {
25 if ( value === undefined ) {
26 return jQuery.curCSS( elem, name );
29 if ( typeof value === "number" && !rexclude.test(name) ) {
33 jQuery.style( elem, name, value );
38 style: function( elem, name, value ) {
39 // don't set styles on text and comment nodes
40 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
44 // ignore negative width and height values #1599
45 if ( (name === "width" || name === "height") && parseFloat(value) < 0 ) {
49 var style = elem.style || elem, set = value !== undefined;
51 // IE uses filters for opacity
52 if ( !jQuery.support.opacity && name === "opacity" ) {
54 // IE has trouble with opacity if it does not have layout
55 // Force it by setting the zoom level
58 // Set the alpha filter to set the opacity
59 var opacity = parseInt( value, 10 ) + "" === "NaN" ? "" : "alpha(opacity=" + value * 100 + ")";
60 var filter = style.filter || jQuery.curCSS( elem, "filter" ) || "";
61 style.filter = ralpha.test(filter) ? filter.replace(ralpha, opacity) : opacity;
64 return style.filter && style.filter.indexOf("opacity=") >= 0 ?
65 (parseFloat( ropacity.exec(style.filter)[1] ) / 100) + "":
69 // Make sure we're using the right name for getting the float value
70 if ( rfloat.test( name ) ) {
74 name = name.replace(rdashAlpha, fcamelCase);
77 style[ name ] = value;
83 css: function( elem, name, force, extra ) {
84 if ( name === "width" || name === "height" ) {
85 var val, props = cssShow, which = name === "width" ? cssWidth : cssHeight;
88 val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
90 if ( extra === "border" ) {
94 jQuery.each( which, function() {
96 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
99 if ( extra === "margin" ) {
100 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
102 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
107 if ( elem.offsetWidth !== 0 ) {
110 jQuery.swap( elem, props, getWH );
113 return Math.max(0, Math.round(val));
116 return jQuery.curCSS( elem, name, force );
119 curCSS: function( elem, name, force ) {
120 var ret, style = elem.style, filter;
122 // IE uses filters for opacity
123 if ( !jQuery.support.opacity && name === "opacity" && elem.currentStyle ) {
124 ret = ropacity.test(elem.currentStyle.filter || "") ?
125 (parseFloat(RegExp.$1) / 100) + "" :
133 // Make sure we're using the right name for getting the float value
134 if ( rfloat.test( name ) ) {
138 if ( !force && style && style[ name ] ) {
141 } else if ( getComputedStyle ) {
143 // Only "float" is needed here
144 if ( rfloat.test( name ) ) {
148 name = name.replace( rupper, "-$1" ).toLowerCase();
150 var defaultView = elem.ownerDocument.defaultView;
152 if ( !defaultView ) {
156 var computedStyle = defaultView.getComputedStyle( elem, null );
158 if ( computedStyle ) {
159 ret = computedStyle.getPropertyValue( name );
162 // We should always get a number back from opacity
163 if ( name === "opacity" && ret === "" ) {
167 } else if ( elem.currentStyle ) {
168 var camelCase = name.replace(rdashAlpha, fcamelCase);
170 ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
172 // From the awesome hack by Dean Edwards
173 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
175 // If we're not dealing with a regular pixel number
176 // but a number that has a weird ending, we need to convert it to pixels
177 if ( !rnumpx.test( ret ) && rnum.test( ret ) ) {
178 // Remember the original values
179 var left = style.left, rsLeft = elem.runtimeStyle.left;
181 // Put in the new values to get a computed value out
182 elem.runtimeStyle.left = elem.currentStyle.left;
183 style.left = camelCase === "fontSize" ? "1em" : (ret || 0);
184 ret = style.pixelLeft + "px";
186 // Revert the changed values
188 elem.runtimeStyle.left = rsLeft;
195 // A method for quickly swapping in/out CSS properties to get correct calculations
196 swap: function( elem, options, callback ) {
199 // Remember the old values, and insert the new ones
200 for ( var name in options ) {
201 old[ name ] = elem.style[ name ];
202 elem.style[ name ] = options[ name ];
205 callback.call( elem );
207 // Revert the old values
208 for ( var name in options ) {
209 elem.style[ name ] = old[ name ];
214 if ( jQuery.expr && jQuery.expr.filters ) {
215 jQuery.expr.filters.hidden = function( elem ) {
216 var width = elem.offsetWidth, height = elem.offsetHeight,
217 skip = elem.nodeName.toLowerCase() === "tr";
219 return width === 0 && height === 0 && !skip ?
221 width > 0 && height > 0 && !skip ?
223 jQuery.curCSS(elem, "display") === "none";
226 jQuery.expr.filters.visible = function( elem ) {
227 return !jQuery.expr.filters.hidden( elem );