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 jQuery.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 );
40 get: function( elem, force ) {
41 var style = elem.style;
42 if ( jQuery.support.opacity && !style.filter ) {
43 return false; // move along, nothing to see here
46 // IE uses filters for opacity
47 var ret = ropacity.test(elem.currentStyle.filter || "") ?
48 (parseFloat(RegExp.$1) / 100) + "" :
56 set: function( elem, value ) {
57 var style = elem.style;
58 if ( jQuery.support.opacity && !style.filter ) {
59 return false; // move along, nothing to see here
62 // IE has trouble with opacity if it does not have layout
63 // Force it by setting the zoom level
66 // Set the alpha filter to set the opacity
67 var opacity = parseInt( value, 10 ) + "" === "NaN" ? "" : "alpha(opacity=" + value * 100 + ")";
68 var filter = style.filter || jQuery.curCSS( elem, "filter" ) || "";
69 style.filter = ralpha.test(filter) ? filter.replace(ralpha, opacity) : opacity;
74 style: function( elem, name, value ) {
75 // don't set styles on text and comment nodes
76 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
80 // ignore negative width and height values #1599
81 if ( (name === "width" || name === "height") && parseFloat(value) < 0 ) {
85 var style = elem.style || elem, set = value !== undefined;
87 // Make sure we're using the right name for getting the float value
88 if ( rfloat.test( name ) ) {
92 name = name.replace(rdashAlpha, fcamelCase);
94 var hooks = jQuery.cssHooks[name] || {};
97 if ( !('set' in hooks) || hooks.set( elem, value ) === false ) {
98 style[ name ] = value;
102 if ( 'get' in hooks ) {
103 var cssHookReturn = hooks.get( elem, false );
104 if ( cssHookReturn !== false ) {
105 return cssHookReturn;
109 return style[ name ];
112 css: function( elem, name, force, extra ) {
113 if ( name === "width" || name === "height" ) {
114 if ( elem.offsetWidth !== 0 ) {
115 val = getWH( elem, name, extra );
118 jQuery.swap( elem, cssShow, function() {
119 val = getWH( elem, name, extra );
123 return Math.max(0, Math.round(val));
126 return jQuery.curCSS( elem, name, force );
129 curCSS: function( elem, name, force ) {
130 var ret, style = elem.style, filter, hooks = jQuery.cssHooks[name] || {};
132 // Make sure we're using the right name for getting the float value
133 if ( rfloat.test( name ) ) {
137 if ( 'get' in hooks && ( ret = hooks.get( elem, force ) ) !== false ) {
141 if ( !force && style && style[ name ] ) {
144 } else if ( getComputedStyle ) {
146 // Only "float" is needed here
147 if ( rfloat.test( name ) ) {
151 name = name.replace( rupper, "-$1" ).toLowerCase();
153 var defaultView = elem.ownerDocument.defaultView;
155 if ( !defaultView ) {
159 var computedStyle = defaultView.getComputedStyle( elem, null );
161 if ( computedStyle ) {
162 ret = computedStyle.getPropertyValue( name );
165 // We should always get a number back from opacity
166 if ( name === "opacity" && ret === "" ) {
170 } else if ( elem.currentStyle ) {
171 var camelCase = name.replace(rdashAlpha, fcamelCase);
173 ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
175 // From the awesome hack by Dean Edwards
176 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
178 // If we're not dealing with a regular pixel number
179 // but a number that has a weird ending, we need to convert it to pixels
180 if ( !rnumpx.test( ret ) && rnum.test( ret ) ) {
181 // Remember the original values
182 var left = style.left, rsLeft = elem.runtimeStyle.left;
184 // Put in the new values to get a computed value out
185 elem.runtimeStyle.left = elem.currentStyle.left;
186 style.left = camelCase === "fontSize" ? "1em" : (ret || 0);
187 ret = style.pixelLeft + "px";
189 // Revert the changed values
191 elem.runtimeStyle.left = rsLeft;
198 // A method for quickly swapping in/out CSS properties to get correct calculations
199 swap: function( elem, options, callback ) {
202 // Remember the old values, and insert the new ones
203 for ( var name in options ) {
204 old[ name ] = elem.style[ name ];
205 elem.style[ name ] = options[ name ];
208 callback.call( elem );
210 // Revert the old values
211 for ( name in options ) {
212 elem.style[ name ] = old[ name ];
217 function getWH( elem, name, extra ) {
218 var which = name === "width" ? cssWidth : cssHeight,
219 val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
221 if ( extra === "border" ) {
225 jQuery.each( which, function() {
227 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
230 if ( extra === "margin" ) {
231 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
234 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
241 if ( jQuery.expr && jQuery.expr.filters ) {
242 jQuery.expr.filters.hidden = function( elem ) {
243 var width = elem.offsetWidth, height = elem.offsetHeight,
244 skip = elem.nodeName.toLowerCase() === "tr";
246 return width === 0 && height === 0 && !skip ?
248 width > 0 && height > 0 && !skip ?
250 jQuery.curCSS(elem, "display") === "none";
253 jQuery.expr.filters.visible = function( elem ) {
254 return !jQuery.expr.filters.hidden( elem );