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" ],
16 // cache check for defaultView.getComputedStyle
17 getComputedStyle = document.defaultView && document.defaultView.getComputedStyle,
18 // normalize float css property
19 styleFloat = jQuery.support.cssFloat ? "cssFloat" : "styleFloat",
20 fcamelCase = function( all, letter ) {
21 return letter.toUpperCase();
24 jQuery.fn.css = function( name, value ) {
25 return jQuery.access( this, name, value, true, function( elem, name, value ) {
26 jQuery.css( elem, name, value );
33 css: function( elem, name, value, force, extra ) {
34 // don't set styles on text and comment nodes
35 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
39 // Make sure we're using the right name for getting the float value
40 if ( rfloat.test( name ) ) {
44 name = name.replace( rdashAlpha, fcamelCase );
46 var ret, style = elem.style || {}, hooks = jQuery.cssHooks[name] || {};
48 if ( value !== undefined ) {
49 if ( typeof value === "number" && !rexclude.test(name) ) {
53 if ( !("set" in hooks) || (value = hooks.set( elem, value )) === false ) {
54 style[ name ] = value;
58 if ( "get" in hooks && (ret = hooks.get( elem, force, extra )) !== false ) {
62 if ( !force && name in style ) {
65 } else if ( curCSS ) {
66 ret = curCSS( elem, name );
73 // A method for quickly swapping in/out CSS properties to get correct calculations
74 swap: function( elem, options, callback ) {
77 // Remember the old values, and insert the new ones
78 for ( var name in options ) {
79 old[ name ] = elem.style[ name ];
80 elem.style[ name ] = options[ name ];
83 callback.call( elem );
85 // Revert the old values
86 for ( name in options ) {
87 elem.style[ name ] = old[ name ];
92 jQuery.each(["height", "width"], function( i, name ) {
93 jQuery.cssHooks[ name ] = {
94 get: function( elem, force, extra ) {
95 if ( elem.offsetWidth !== 0 ) {
96 val = getWH( elem, name, extra );
99 jQuery.swap( elem, cssShow, function() {
100 val = getWH( elem, name, extra );
105 set: function( elem, value ) {
106 // ignore negative width and height values #1599
107 elem.style[ name ] = Math.max( parseFloat(value), 0 );
112 if ( !jQuery.support.opacity ) {
113 jQuery.cssHooks.opacity = {
114 get: function( elem, force ) {
115 // IE uses filters for opacity
116 return ropacity.test(elem.currentStyle.filter || "") ?
117 (parseFloat(RegExp.$1) / 100) + "" :
121 set: function( elem, value ) {
122 var style = elem.style;
124 // IE has trouble with opacity if it does not have layout
125 // Force it by setting the zoom level
128 // Set the alpha filter to set the opacity
129 var opacity = parseInt( value, 10 ) + "" === "NaN" ?
131 "alpha(opacity=" + value * 100 + ")";
133 var filter = style.filter || jQuery.css( elem, "filter" ) || "";
135 style.filter = ralpha.test(filter) ?
136 filter.replace(ralpha, opacity) :
142 if ( getComputedStyle ) {
143 curCSS = function( elem, name ) {
144 var ret, defaultView, computedStyle;
146 // Only "float" is needed here
147 if ( rfloat.test( name ) ) {
151 name = name.replace( rupper, "-$1" ).toLowerCase();
153 if ( !(defaultView = elem.ownerDocument.defaultView) ) {
157 if ( (computedStyle = defaultView.getComputedStyle( elem, null )) ) {
158 ret = computedStyle.getPropertyValue( name );
161 // We should always get a number back from opacity
162 if ( name === "opacity" && ret === "" ) {
169 } else if ( document.documentElement.currentStyle ) {
170 curCSS = function( elem, name ) {
171 var left, rsLeft, ret = elem.currentStyle[ name ];
173 // From the awesome hack by Dean Edwards
174 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
176 // If we're not dealing with a regular pixel number
177 // but a number that has a weird ending, we need to convert it to pixels
178 if ( !rnumpx.test( ret ) && rnum.test( ret ) ) {
179 // Remember the original values
181 rsLeft = elem.runtimeStyle.left;
183 // Put in the new values to get a computed value out
184 elem.runtimeStyle.left = elem.currentStyle.left;
185 style.left = name === "fontSize" ? "1em" : (ret || 0);
186 ret = style.pixelLeft + "px";
188 // Revert the changed values
190 elem.runtimeStyle.left = rsLeft;
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.css( elem, "padding" + this, undefined, true)) || 0;
210 if ( extra === "margin" ) {
211 val += parseFloat(jQuery.css( elem, "margin" + this, undefined, true)) || 0;
214 val -= parseFloat(jQuery.css( elem, "border" + this + "Width", undefined, 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.css(elem, "display") === "none";
233 jQuery.expr.filters.visible = function( elem ) {
234 return !jQuery.expr.filters.hidden( elem );