Commit 65b24861 authored by Brandon Aaron's avatar Brandon Aaron

First look at css hooks. These hooks provide a way to change how jQuery...

First look at css hooks. These hooks provide a way to change how jQuery handles getting and setting certain css properties. This means normalizing properties like background-position can easily be done via plugins.

They are similar in concept to the special event hooks. Return false from the hook to revert control back to jQuery's normal processes for getting and setting certain css properties.
parent 97cbd766
...@@ -35,22 +35,30 @@ jQuery.fn.css = function( name, value ) { ...@@ -35,22 +35,30 @@ jQuery.fn.css = function( name, value ) {
}; };
jQuery.extend({ jQuery.extend({
style: function( elem, name, value ) { cssHooks: {
// don't set styles on text and comment nodes opacity: {
if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) { get: function( elem, force ) {
return undefined; var style = elem.style;
if ( jQuery.support.opacity && !style.filter ) {
return false; // move along, nothing to see here
} }
// ignore negative width and height values #1599 // IE uses filters for opacity
if ( (name === "width" || name === "height") && parseFloat(value) < 0 ) { var ret = ropacity.test(elem.currentStyle.filter || "") ?
value = undefined; (parseFloat(RegExp.$1) / 100) + "" :
} "";
var style = elem.style || elem, set = value !== undefined; return ret === "" ?
"1" :
ret;
},
set: function( elem, value ) {
var style = elem.style;
if ( jQuery.support.opacity && !style.filter ) {
return false; // move along, nothing to see here
}
// IE uses filters for opacity
if ( !jQuery.support.opacity && name === "opacity" && style.filter ) {
if ( set ) {
// IE has trouble with opacity if it does not have layout // IE has trouble with opacity if it does not have layout
// Force it by setting the zoom level // Force it by setting the zoom level
style.zoom = 1; style.zoom = 1;
...@@ -60,12 +68,22 @@ jQuery.extend({ ...@@ -60,12 +68,22 @@ jQuery.extend({
var filter = style.filter || jQuery.curCSS( elem, "filter" ) || ""; var filter = style.filter || jQuery.curCSS( elem, "filter" ) || "";
style.filter = ralpha.test(filter) ? filter.replace(ralpha, opacity) : opacity; style.filter = ralpha.test(filter) ? filter.replace(ralpha, opacity) : opacity;
} }
}
},
return style.filter && style.filter.indexOf("opacity=") >= 0 ? style: function( elem, name, value ) {
(parseFloat( ropacity.exec(style.filter)[1] ) / 100) + "": // don't set styles on text and comment nodes
""; if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
return undefined;
} }
// ignore negative width and height values #1599
if ( (name === "width" || name === "height") && parseFloat(value) < 0 ) {
value = undefined;
}
var style = elem.style || elem, set = value !== undefined;
// Make sure we're using the right name for getting the float value // Make sure we're using the right name for getting the float value
if ( rfloat.test( name ) ) { if ( rfloat.test( name ) ) {
name = styleFloat; name = styleFloat;
...@@ -73,9 +91,20 @@ jQuery.extend({ ...@@ -73,9 +91,20 @@ jQuery.extend({
name = name.replace(rdashAlpha, fcamelCase); name = name.replace(rdashAlpha, fcamelCase);
var hooks = jQuery.cssHooks[name] || {};
if ( set ) { if ( set ) {
if ( !('set' in hooks) || hooks.set( elem, value ) === false ) {
style[ name ] = value; style[ name ] = value;
} }
}
if ( 'get' in hooks ) {
var cssHookReturn = hooks.get( elem, false );
if ( cssHookReturn !== false ) {
return cssHookReturn;
}
}
return style[ name ]; return style[ name ];
}, },
...@@ -98,24 +127,17 @@ jQuery.extend({ ...@@ -98,24 +127,17 @@ jQuery.extend({
}, },
curCSS: function( elem, name, force ) { curCSS: function( elem, name, force ) {
var ret, style = elem.style, filter; var ret, style = elem.style, filter, hooks = jQuery.cssHooks[name] || {};
// IE uses filters for opacity
if ( !jQuery.support.opacity && name === "opacity" && elem.currentStyle ) {
ret = ropacity.test(elem.currentStyle.filter || "") ?
(parseFloat(RegExp.$1) / 100) + "" :
"";
return ret === "" ?
"1" :
ret;
}
// Make sure we're using the right name for getting the float value // Make sure we're using the right name for getting the float value
if ( rfloat.test( name ) ) { if ( rfloat.test( name ) ) {
name = styleFloat; name = styleFloat;
} }
if ( 'get' in hooks && ( ret = hooks.get( elem, force ) ) !== false ) {
return ret;
}
if ( !force && style && style[ name ] ) { if ( !force && style && style[ name ] ) {
ret = style[ name ]; ret = style[ name ];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment