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";
8 jQuery.fn.css = function( name, value ) {
9 var options = name, isFunction = jQuery.isFunction( value );
11 if ( typeof name === "string" ) {
12 // Are we setting the style?
13 if ( value === undefined ) {
15 jQuery.css( this[0], name ) :
18 // Convert name, value params to options hash format
21 options[ name ] = value;
25 // For each element...
26 for ( var i = 0, l = this.length; i < l; i++ ) {
30 for ( var prop in options ) {
31 value = options[prop];
34 value = value.call( elem, i );
37 if ( typeof value === "number" && !exclude.test(prop) ) {
41 jQuery.style( elem, prop, value );
49 style: function( elem, name, value ) {
50 // don't set styles on text and comment nodes
51 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
54 // ignore negative width and height values #1599
55 if ( (name == 'width' || name == 'height') && parseFloat(value) < 0 )
58 var style = elem.style || elem, set = value !== undefined;
60 // IE uses filters for opacity
61 if ( !jQuery.support.opacity && name == "opacity" ) {
63 // IE has trouble with opacity if it does not have layout
64 // Force it by setting the zoom level
67 // Set the alpha filter to set the opacity
68 style.filter = (style.filter || "").replace( /alpha\([^)]*\)/, "" ) +
69 (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
72 return style.filter && style.filter.indexOf("opacity=") >= 0 ?
73 (parseFloat( style.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '':
77 // Make sure we're using the right name for getting the float value
78 if ( /float/i.test( name ) )
81 name = name.replace(/-([a-z])/ig, function(all, letter){
82 return letter.toUpperCase();
86 style[ name ] = value;
91 css: function( elem, name, force, extra ) {
92 if ( name == "width" || name == "height" ) {
93 var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
96 val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
98 if ( extra === "border" )
101 jQuery.each( which, function() {
103 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
104 if ( extra === "margin" )
105 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
107 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
111 if ( elem.offsetWidth !== 0 )
114 jQuery.swap( elem, props, getWH );
116 return Math.max(0, Math.round(val));
119 return jQuery.curCSS( elem, name, force );
122 curCSS: function( elem, name, force ) {
123 var ret, style = elem.style, filter;
125 // IE uses filters for opacity
126 if ( !jQuery.support.opacity && name === "opacity" && elem.currentStyle ) {
127 ret = (elem.currentStyle.filter || "").match(/opacity=([^)]*)/) ?
128 (parseFloat(RegExp.$1) / 100) + "" :
136 // Make sure we're using the right name for getting the float value
137 if ( /float/i.test( name ) )
140 if ( !force && style && style[ name ] ) {
143 } else if ( getComputedStyle ) {
145 // Only "float" is needed here
146 if ( /float/i.test( name ) )
149 name = name.replace( /([A-Z])/g, "-$1" ).toLowerCase();
151 var computedStyle = elem.ownerDocument.defaultView.getComputedStyle( elem, null );
154 ret = computedStyle.getPropertyValue( name );
156 // We should always get a number back from opacity
157 if ( name == "opacity" && ret == "" )
160 } else if ( elem.currentStyle ) {
161 var camelCase = name.replace(/\-(\w)/g, function(all, letter){
162 return letter.toUpperCase();
165 ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
167 // From the awesome hack by Dean Edwards
168 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
170 // If we're not dealing with a regular pixel number
171 // but a number that has a weird ending, we need to convert it to pixels
172 if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) {
173 // Remember the original values
174 var left = style.left, rsLeft = elem.runtimeStyle.left;
176 // Put in the new values to get a computed value out
177 elem.runtimeStyle.left = elem.currentStyle.left;
178 style.left = ret || 0;
179 ret = style.pixelLeft + "px";
181 // Revert the changed values
183 elem.runtimeStyle.left = rsLeft;
190 // A method for quickly swapping in/out CSS properties to get correct calculations
191 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 ];