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 // cache check for defaultView.getComputedStyle
12 getComputedStyle = document.defaultView && document.defaultView.getComputedStyle,
13 // normalize float css property
14 styleFloat = jQuery.support.cssFloat ? "cssFloat" : "styleFloat",
15 fcamelCase = function(all, letter){
16 return letter.toUpperCase();
19 jQuery.fn.css = function( name, value ) {
20 return access( this, name, value, true, function( elem, name, value ) {
21 if ( value === undefined ) {
22 return jQuery.curCSS( elem, name );
25 if ( typeof value === "number" && !rexclude.test(name) ) {
29 jQuery.style( elem, name, value );
34 style: function( elem, name, value ) {
35 // don't set styles on text and comment nodes
36 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
40 // ignore negative width and height values #1599
41 if ( (name === "width" || name === "height") && parseFloat(value) < 0 ) {
45 var style = elem.style || elem, set = value !== undefined;
47 // IE uses filters for opacity
48 if ( !jQuery.support.opacity && name === "opacity" ) {
50 // IE has trouble with opacity if it does not have layout
51 // Force it by setting the zoom level
54 // Set the alpha filter to set the opacity
55 var opacity = parseInt( value, 10 ) + '' === "NaN" ? "" : "alpha(opacity=" + value * 100 + ")";
56 var filter = style.filter || jQuery.curCSS( elem, 'filter' ) || "";
57 style.filter = ralpha.test(filter) ? filter.replace(ralpha, opacity) : opacity;
60 return style.filter && style.filter.indexOf("opacity=") >= 0 ?
61 (parseFloat( ropacity.exec(style.filter)[1] ) / 100) + '':
65 // Make sure we're using the right name for getting the float value
66 if ( rfloat.test( name ) ) {
70 name = name.replace(rdashAlpha, fcamelCase);
73 style[ name ] = value;
79 css: function( elem, name, force, extra ) {
80 if ( name === "width" || name === "height" ) {
81 var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name === "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
84 val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
86 if ( extra === "border" ) { return; }
88 jQuery.each( which, function() {
90 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
93 if ( extra === "margin" ) {
94 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
96 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
101 if ( elem.offsetWidth !== 0 ) {
104 jQuery.swap( elem, props, getWH );
107 return Math.max(0, Math.round(val));
110 return jQuery.curCSS( elem, name, force );
113 curCSS: function( elem, name, force ) {
114 var ret, style = elem.style, filter;
116 // IE uses filters for opacity
117 if ( !jQuery.support.opacity && name === "opacity" && elem.currentStyle ) {
118 ret = ropacity.test(elem.currentStyle.filter || "") ?
119 (parseFloat(RegExp.$1) / 100) + "" :
127 // Make sure we're using the right name for getting the float value
128 if ( rfloat.test( name ) ) {
132 if ( !force && style && style[ name ] ) {
135 } else if ( getComputedStyle ) {
137 // Only "float" is needed here
138 if ( rfloat.test( name ) ) {
142 name = name.replace( rupper, "-$1" ).toLowerCase();
144 var defaultView = elem.ownerDocument.defaultView;
146 if ( !defaultView ) {
150 var computedStyle = defaultView.getComputedStyle( elem, null );
152 if ( computedStyle ) {
153 ret = computedStyle.getPropertyValue( name );
156 // We should always get a number back from opacity
157 if ( name === "opacity" && ret === "" ) {
161 } else if ( elem.currentStyle ) {
162 var camelCase = name.replace(rdashAlpha, fcamelCase);
164 ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
166 // From the awesome hack by Dean Edwards
167 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
169 // If we're not dealing with a regular pixel number
170 // but a number that has a weird ending, we need to convert it to pixels
171 if ( !rnumpx.test( ret ) && rnum.test( ret ) ) {
172 // Remember the original values
173 var left = style.left, rsLeft = elem.runtimeStyle.left;
175 // Put in the new values to get a computed value out
176 elem.runtimeStyle.left = elem.currentStyle.left;
177 style.left = camelCase === "fontSize" ? "1em" : (ret || 0);
178 ret = style.pixelLeft + "px";
180 // Revert the changed values
182 elem.runtimeStyle.left = rsLeft;
189 // A method for quickly swapping in/out CSS properties to get correct calculations
190 swap: function( elem, options, callback ) {
193 // Remember the old values, and insert the new ones
194 for ( var name in options ) {
195 old[ name ] = elem.style[ name ];
196 elem.style[ name ] = options[ name ];
199 callback.call( elem );
201 // Revert the old values
202 for ( var name in options ) {
203 elem.style[ name ] = old[ name ];
208 if ( jQuery.expr && jQuery.expr.filters ) {
209 jQuery.expr.filters.hidden = function( elem ) {
210 var width = elem.offsetWidth, height = elem.offsetHeight,
211 skip = elem.nodeName.toLowerCase() === "tr";
213 return width === 0 && height === 0 && !skip ?
215 width > 0 && height > 0 && !skip ?
217 jQuery.curCSS(elem, "display") === "none";
220 jQuery.expr.filters.visible = function( elem ) {
221 return !jQuery.expr.filters.hidden( elem );