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;
27 // For each value, determine whether it's a Function so we don't
28 // need to determine it again for each element
29 for ( var prop in options )
30 isFunction[prop] = jQuery.isFunction( options[prop] );
32 // For each element...
33 for ( var i = 0, l = this.length; i < l; i++ ) {
37 for ( var prop in options ) {
38 value = options[prop];
40 if ( isFunction[prop] ) value = value.call( elem, i );
42 if ( typeof value === "number" && !exclude.test(prop) ) {
46 jQuery.style( elem, prop, value );
54 style: function( elem, name, value ) {
55 // don't set styles on text and comment nodes
56 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
59 // ignore negative width and height values #1599
60 if ( (name == 'width' || name == 'height') && parseFloat(value) < 0 )
63 var style = elem.style || elem, set = value !== undefined;
65 // IE uses filters for opacity
66 if ( !jQuery.support.opacity && name == "opacity" ) {
68 // IE has trouble with opacity if it does not have layout
69 // Force it by setting the zoom level
72 // Set the alpha filter to set the opacity
73 style.filter = (style.filter || "").replace( /alpha\([^)]*\)/, "" ) +
74 (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
77 return style.filter && style.filter.indexOf("opacity=") >= 0 ?
78 (parseFloat( /opacity=([^)]*)/.exec(style.filter)[1] ) / 100) + '':
82 // Make sure we're using the right name for getting the float value
83 if ( /float/i.test( name ) )
86 name = name.replace(/-([a-z])/ig, function(all, letter){
87 return letter.toUpperCase();
91 style[ name ] = value;
96 css: function( elem, name, force, extra ) {
97 if ( name == "width" || name == "height" ) {
98 var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
101 val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
103 if ( extra === "border" )
106 jQuery.each( which, function() {
108 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
109 if ( extra === "margin" )
110 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
112 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
116 if ( elem.offsetWidth !== 0 )
119 jQuery.swap( elem, props, getWH );
121 return Math.max(0, Math.round(val));
124 return jQuery.curCSS( elem, name, force );
127 curCSS: function( elem, name, force ) {
128 var ret, style = elem.style, filter;
130 // IE uses filters for opacity
131 if ( !jQuery.support.opacity && name === "opacity" && elem.currentStyle ) {
132 ret = /opacity=([^)]*)/.test(elem.currentStyle.filter || "") ?
133 (parseFloat(RegExp.$1) / 100) + "" :
141 // Make sure we're using the right name for getting the float value
142 if ( /float/i.test( name ) )
145 if ( !force && style && style[ name ] ) {
148 } else if ( getComputedStyle ) {
150 // Only "float" is needed here
151 if ( /float/i.test( name ) )
154 name = name.replace( /([A-Z])/g, "-$1" ).toLowerCase();
156 var computedStyle = elem.ownerDocument.defaultView.getComputedStyle( elem, null );
159 ret = computedStyle.getPropertyValue( name );
161 // We should always get a number back from opacity
162 if ( name == "opacity" && ret == "" )
165 } else if ( elem.currentStyle ) {
166 var camelCase = name.replace(/\-(\w)/g, function(all, letter){
167 return letter.toUpperCase();
170 ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
172 // From the awesome hack by Dean Edwards
173 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
175 // If we're not dealing with a regular pixel number
176 // but a number that has a weird ending, we need to convert it to pixels
177 if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) {
178 // Remember the original values
179 var left = style.left, rsLeft = elem.runtimeStyle.left;
181 // Put in the new values to get a computed value out
182 elem.runtimeStyle.left = elem.currentStyle.left;
183 style.left = ret || 0;
184 ret = style.pixelLeft + "px";
186 // Revert the changed values
188 elem.runtimeStyle.left = rsLeft;
195 // A method for quickly swapping in/out CSS properties to get correct calculations
196 swap: function( elem, options, callback ) {
198 // Remember the old values, and insert the new ones
199 for ( var name in options ) {
200 old[ name ] = elem.style[ name ];
201 elem.style[ name ] = options[ name ];
204 callback.call( elem );
206 // Revert the old values
207 for ( var name in options )
208 elem.style[ name ] = old[ name ];