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] );
35 // For each element...
36 for ( var i = 0, l = this.length; i < l; i++ ) {
40 for ( var prop in options ) {
41 value = options[prop];
43 if ( isFunction[prop] ) value = value.call( elem, i );
45 if ( typeof value === "number" && !exclude.test(prop) ) {
49 jQuery.style( elem, prop, value );
57 style: function( elem, name, value ) {
58 // don't set styles on text and comment nodes
59 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
62 // ignore negative width and height values #1599
63 if ( (name == 'width' || name == 'height') && parseFloat(value) < 0 )
66 var style = elem.style || elem, set = value !== undefined;
68 // IE uses filters for opacity
69 if ( !jQuery.support.opacity && name == "opacity" ) {
71 // IE has trouble with opacity if it does not have layout
72 // Force it by setting the zoom level
75 // Set the alpha filter to set the opacity
76 style.filter = (style.filter || "").replace( /alpha\([^)]*\)/, "" ) +
77 (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
80 return style.filter && style.filter.indexOf("opacity=") >= 0 ?
81 (parseFloat( /opacity=([^)]*)/.exec(style.filter)[1] ) / 100) + '':
85 // Make sure we're using the right name for getting the float value
86 if ( /float/i.test( name ) )
89 name = name.replace(/-([a-z])/ig, fcamelCase);
92 style[ name ] = value;
97 css: function( elem, name, force, extra ) {
98 if ( name == "width" || name == "height" ) {
99 var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
102 val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
104 if ( extra === "border" )
107 jQuery.each( which, function() {
109 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
110 if ( extra === "margin" )
111 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
113 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
117 if ( elem.offsetWidth !== 0 )
120 jQuery.swap( elem, props, getWH );
122 return Math.max(0, Math.round(val));
125 return jQuery.curCSS( elem, name, force );
128 curCSS: function( elem, name, force ) {
129 var ret, style = elem.style, filter;
131 // IE uses filters for opacity
132 if ( !jQuery.support.opacity && name === "opacity" && elem.currentStyle ) {
133 ret = /opacity=([^)]*)/.test(elem.currentStyle.filter || "") ?
134 (parseFloat(RegExp.$1) / 100) + "" :
142 // Make sure we're using the right name for getting the float value
143 if ( /float/i.test( name ) )
146 if ( !force && style && style[ name ] ) {
149 } else if ( getComputedStyle ) {
151 // Only "float" is needed here
152 if ( /float/i.test( name ) )
155 name = name.replace( /([A-Z])/g, "-$1" ).toLowerCase();
157 var computedStyle = elem.ownerDocument.defaultView.getComputedStyle( elem, null );
160 ret = computedStyle.getPropertyValue( name );
162 // We should always get a number back from opacity
163 if ( name == "opacity" && ret == "" )
166 } else if ( elem.currentStyle ) {
167 var camelCase = name.replace(/\-(\w)/g, fcamelCase);
169 ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
171 // From the awesome hack by Dean Edwards
172 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
174 // If we're not dealing with a regular pixel number
175 // but a number that has a weird ending, we need to convert it to pixels
176 if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) {
177 // Remember the original values
178 var left = style.left, rsLeft = elem.runtimeStyle.left;
180 // Put in the new values to get a computed value out
181 elem.runtimeStyle.left = elem.currentStyle.left;
182 style.left = ret || 0;
183 ret = style.pixelLeft + "px";
185 // Revert the changed values
187 elem.runtimeStyle.left = rsLeft;
194 // A method for quickly swapping in/out CSS properties to get correct calculations
195 swap: function( elem, options, callback ) {
197 // Remember the old values, and insert the new ones
198 for ( var name in options ) {
199 old[ name ] = elem.style[ name ];
200 elem.style[ name ] = options[ name ];
203 callback.call( elem );
205 // Revert the old values
206 for ( var name in options )
207 elem.style[ name ] = old[ name ];