1 // exclude the following css properties to add px
2 var exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i,
3 // cache check for defaultView.getComputedStyle
4 getComputedStyle = document.defaultView && document.defaultView.getComputedStyle,
5 // normalize float css property
6 styleFloat = jQuery.support.cssFloat ? "cssFloat" : "styleFloat",
7 fcamelCase = function(all, letter){
8 return letter.toUpperCase();
11 jQuery.fn.css = function( name, value ) {
12 var options = name, isFunction = jQuery.isFunction( value );
14 if ( typeof name === "string" ) {
15 // Are we setting the style?
16 if ( value === undefined ) {
18 jQuery.css( this[0], name ) :
21 // Convert name, value params to options hash format
24 options[ name ] = value;
30 // For each value, determine whether it's a Function so we don't
31 // need to determine it again for each element
32 for ( var prop in options ) {
33 isFunction[prop] = jQuery.isFunction( options[prop] );
36 // For each element...
37 for ( var i = 0, l = this.length; i < l; i++ ) {
41 for ( var prop in options ) {
42 value = options[prop];
44 if ( isFunction[prop] ) {
45 value = value.call( elem, i );
48 if ( typeof value === "number" && !exclude.test(prop) ) {
52 jQuery.style( elem, prop, value );
60 style: function( elem, name, value ) {
61 // don't set styles on text and comment nodes
62 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
66 // ignore negative width and height values #1599
67 if ( (name === "width" || name === "height") && parseFloat(value) < 0 ) {
71 var style = elem.style || elem, set = value !== undefined;
73 // IE uses filters for opacity
74 if ( !jQuery.support.opacity && name === "opacity" ) {
76 // IE has trouble with opacity if it does not have layout
77 // Force it by setting the zoom level
80 // Set the alpha filter to set the opacity
81 style.filter = (style.filter || "").replace( /alpha\([^)]*\)/, "" ) +
82 (parseInt( value ) + '' === "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
85 return style.filter && style.filter.indexOf("opacity=") >= 0 ?
86 (parseFloat( /opacity=([^)]*)/.exec(style.filter)[1] ) / 100) + '':
90 // Make sure we're using the right name for getting the float value
91 if ( /float/i.test( name ) ) {
95 name = name.replace(/-([a-z])/ig, fcamelCase);
98 style[ name ] = value;
101 return style[ name ];
104 css: function( elem, name, force, extra ) {
105 if ( name === "width" || name === "height" ) {
106 var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name === "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
109 val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
111 if ( extra === "border" ) { return; }
113 jQuery.each( which, function() {
115 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
118 if ( extra === "margin" ) {
119 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
121 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
126 if ( elem.offsetWidth !== 0 ) {
129 jQuery.swap( elem, props, getWH );
132 return Math.max(0, Math.round(val));
135 return jQuery.curCSS( elem, name, force );
138 curCSS: function( elem, name, force ) {
139 var ret, style = elem.style, filter;
141 // IE uses filters for opacity
142 if ( !jQuery.support.opacity && name === "opacity" && elem.currentStyle ) {
143 ret = /opacity=([^)]*)/.test(elem.currentStyle.filter || "") ?
144 (parseFloat(RegExp.$1) / 100) + "" :
152 // Make sure we're using the right name for getting the float value
153 if ( /float/i.test( name ) ) {
157 if ( !force && style && style[ name ] ) {
160 } else if ( getComputedStyle ) {
162 // Only "float" is needed here
163 if ( /float/i.test( name ) ) {
167 name = name.replace( /([A-Z])/g, "-$1" ).toLowerCase();
169 var computedStyle = elem.ownerDocument.defaultView.getComputedStyle( elem, null );
171 if ( computedStyle ) {
172 ret = computedStyle.getPropertyValue( name );
175 // We should always get a number back from opacity
176 if ( name === "opacity" && ret === "" ) {
180 } else if ( elem.currentStyle ) {
181 var camelCase = name.replace(/\-(\w)/g, fcamelCase);
183 ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
185 // From the awesome hack by Dean Edwards
186 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
188 // If we're not dealing with a regular pixel number
189 // but a number that has a weird ending, we need to convert it to pixels
190 if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) {
191 // Remember the original values
192 var left = style.left, rsLeft = elem.runtimeStyle.left;
194 // Put in the new values to get a computed value out
195 elem.runtimeStyle.left = elem.currentStyle.left;
196 style.left = ret || 0;
197 ret = style.pixelLeft + "px";
199 // Revert the changed values
201 elem.runtimeStyle.left = rsLeft;
208 // A method for quickly swapping in/out CSS properties to get correct calculations
209 swap: function( elem, options, callback ) {
212 // Remember the old values, and insert the new ones
213 for ( var name in options ) {
214 old[ name ] = elem.style[ name ];
215 elem.style[ name ] = options[ name ];
218 callback.call( elem );
220 // Revert the old values
221 for ( var name in options ) {
222 elem.style[ name ] = old[ name ];