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 var options = name, isFunction = jQuery.isFunction( value );
22 if ( typeof name === "string" ) {
23 // Are we setting the style?
24 if ( value === undefined ) {
26 jQuery.css( this[0], name ) :
29 // Convert name, value params to options hash format
32 options[ name ] = value;
38 // For each value, determine whether it's a Function so we don't
39 // need to determine it again for each element
40 for ( var prop in options ) {
41 isFunction[prop] = jQuery.isFunction( options[prop] );
44 // For each element...
45 for ( var i = 0, l = this.length; i < l; i++ ) {
49 for ( var prop in options ) {
50 value = options[prop];
52 if ( isFunction[prop] ) {
53 value = value.call( elem, i );
56 if ( typeof value === "number" && !rexclude.test(prop) ) {
60 jQuery.style( elem, prop, value );
68 style: function( elem, name, value ) {
69 // don't set styles on text and comment nodes
70 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
74 // ignore negative width and height values #1599
75 if ( (name === "width" || name === "height") && parseFloat(value) < 0 ) {
79 var style = elem.style || elem, set = value !== undefined;
81 // IE uses filters for opacity
82 if ( !jQuery.support.opacity && name === "opacity" ) {
84 // IE has trouble with opacity if it does not have layout
85 // Force it by setting the zoom level
88 // Set the alpha filter to set the opacity
89 style.filter = (style.filter || "").replace( ralpha, "" ) +
90 (parseInt( value ) + '' === "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
93 return style.filter && style.filter.indexOf("opacity=") >= 0 ?
94 (parseFloat( ropacity.exec(style.filter)[1] ) / 100) + '':
98 // Make sure we're using the right name for getting the float value
99 if ( rfloat.test( name ) ) {
103 name = name.replace(rdashAlpha, fcamelCase);
106 style[ name ] = value;
109 return style[ name ];
112 css: function( elem, name, force, extra ) {
113 if ( name === "width" || name === "height" ) {
114 var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name === "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
117 val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
119 if ( extra === "border" ) { return; }
121 jQuery.each( which, function() {
123 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
126 if ( extra === "margin" ) {
127 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
129 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
134 if ( elem.offsetWidth !== 0 ) {
137 jQuery.swap( elem, props, getWH );
140 return Math.max(0, Math.round(val));
143 return jQuery.curCSS( elem, name, force );
146 curCSS: function( elem, name, force ) {
147 var ret, style = elem.style, filter;
149 // IE uses filters for opacity
150 if ( !jQuery.support.opacity && name === "opacity" && elem.currentStyle ) {
151 ret = ropacity.test(elem.currentStyle.filter || "") ?
152 (parseFloat(RegExp.$1) / 100) + "" :
160 // Make sure we're using the right name for getting the float value
161 if ( rfloat.test( name ) ) {
165 if ( !force && style && style[ name ] ) {
168 } else if ( getComputedStyle ) {
170 // Only "float" is needed here
171 if ( rfloat.test( name ) ) {
175 name = name.replace( rupper, "-$1" ).toLowerCase();
177 var computedStyle = elem.ownerDocument.defaultView.getComputedStyle( elem, null );
179 if ( computedStyle ) {
180 ret = computedStyle.getPropertyValue( name );
183 // We should always get a number back from opacity
184 if ( name === "opacity" && ret === "" ) {
188 } else if ( elem.currentStyle ) {
189 var camelCase = name.replace(rdashAlpha, fcamelCase);
191 ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
193 // From the awesome hack by Dean Edwards
194 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
196 // If we're not dealing with a regular pixel number
197 // but a number that has a weird ending, we need to convert it to pixels
198 if ( !rnumpx.test( ret ) && rnum.test( ret ) ) {
199 // Remember the original values
200 var left = style.left, rsLeft = elem.runtimeStyle.left;
202 // Put in the new values to get a computed value out
203 elem.runtimeStyle.left = elem.currentStyle.left;
204 style.left = ret || 0;
205 ret = style.pixelLeft + "px";
207 // Revert the changed values
209 elem.runtimeStyle.left = rsLeft;
216 // A method for quickly swapping in/out CSS properties to get correct calculations
217 swap: function( elem, options, callback ) {
220 // Remember the old values, and insert the new ones
221 for ( var name in options ) {
222 old[ name ] = elem.style[ name ];
223 elem.style[ name ] = options[ name ];
226 callback.call( elem );
228 // Revert the old values
229 for ( var name in options ) {
230 elem.style[ name ] = old[ name ];