|
7 | 7 | <p>For example, some versions of Webkit-based browsers require <code>-webkit-border-radius</code> to set the <code>border-radius</code> on an element, while earlier Firefox versions require <code>-moz-border-radius</code>. A css hook can normalize these vendor-prefixed properties to let <code>.css()</code> accept a single, standard property name (<code>border-radius</code>, or with DOM property syntax, <code>borderRadius</code>).</p>
|
8 | 8 | <p>In addition to providing fine-grained control over how specific style properties are handled, <code>$.cssHooks</code> also extends the set of properties available to the <code>.animate()</code> method.</p>
|
9 | 9 | <p>Defining a new css hook is straight-forward. The skeleton template below can serve as a guide to creating your own. </p>
|
10 |
| -<pre class="prettyprint">(function($) { |
| 10 | +<pre><code>(function($) { |
11 | 11 | // first, check to see if cssHooks are supported
|
12 | 12 | if ( !$.cssHooks ) {
|
13 | 13 | // if not, output an error message
|
|
24 | 24 | }
|
25 | 25 | };
|
26 | 26 | })(jQuery);
|
27 |
| -</pre> |
| 27 | +</code></pre> |
28 | 28 | <h4 id="feature-testing">Feature Testing</h4>
|
29 | 29 | <p>Before normalizing a vendor-specific CSS property, first determine whether the browser supports the standard property or a vendor-prefixed variation. For example, to check for support of the <code>border-radius</code> property, see if any variation is a member of a temporary element's <code>style</code> object.</p>
|
30 |
| -<pre class="prettyprint">(function($) { |
| 30 | +<pre><code>(function($) { |
31 | 31 | function styleSupport( prop ) {
|
32 | 32 | var vendorProp, supportedProp,
|
33 | 33 |
|
|
64 | 64 | // call the function, e.g. testing for "border-radius" support:
|
65 | 65 | styleSupport( "borderRadius" );
|
66 | 66 | })(jQuery);
|
67 |
| -</pre> |
| 67 | +</code></pre> |
68 | 68 | <h4 id="defining-complete-csshook">Defining a complete css hook</h4>
|
69 | 69 | <p>To define a complete css hook, combine the support test with a filled-out version of the skeleton template provided in the first example:</p>
|
70 |
| -<pre class="prettyprint">(function($) { |
| 70 | +<pre><code>(function($) { |
71 | 71 | if ( !$.cssHooks ) {
|
72 | 72 | throw("jQuery 1.4.3+ is needed for this plugin to work");
|
73 | 73 | return;
|
|
111 | 111 | };
|
112 | 112 | }
|
113 | 113 | })(jQuery);
|
114 |
| -</pre> |
| 114 | +</code></pre> |
115 | 115 |
|
116 | 116 | <p>You can then set the border radius in a supported browser using either the DOM (camelCased) style or the CSS (hyphenated) style:</p>
|
117 |
| -<pre class="prettyprint"> |
| 117 | +<pre><code> |
118 | 118 | $("#element").css("borderRadius", "10px");
|
119 | 119 | $("#another").css("border-radius", "20px");
|
120 |
| -</pre> |
| 120 | +</code></pre> |
121 | 121 | <p>If the browser lacks support for any form of the CSS property, vendor-prefixed or not, the style is not applied to the element. However, if the browser supports a proprietary alternative, it can be applied to the cssHooks instead. </p>
|
122 |
| -<pre class="prettyprint"> |
| 122 | +<pre><code> |
123 | 123 | (function($) {
|
124 | 124 | // feature test for support of a CSS property
|
125 | 125 | // and a proprietary alternative
|
@@ -150,18 +150,18 @@ $("#another").css("border-radius", "20px");
|
150 | 150 | }
|
151 | 151 |
|
152 | 152 | })(jQuery);
|
153 |
| -</pre> |
| 153 | +</code></pre> |
154 | 154 | <h4 id="special-units">Special units</h4>
|
155 | 155 | <p>By default, jQuery adds a "px" unit to the values passed to the <code>.css()</code> method. This behavior can be prevented by adding the property to the <code>jQuery.cssNumber</code> object</p>
|
156 | 156 |
|
157 |
| -<pre class="prettyprint">$.cssNumber["someCSSProp"] = true;</pre> |
| 157 | +<pre><code>$.cssNumber["someCSSProp"] = true;</code></pre> |
158 | 158 |
|
159 | 159 | <h4 id="animating">Animating with cssHooks</h4>
|
160 | 160 | <p>A css hook can also hook into jQuery's animation mechanism by adding a property to the <code>jQuery.fx.step</code> object:</p>
|
161 |
| -<pre class="prettyprint">$.fx.step["someCSSProp"] = function(fx){ |
| 161 | +<pre><code>$.fx.step["someCSSProp"] = function(fx){ |
162 | 162 | $.cssHooks["someCSSProp"].set( fx.elem, fx.now + fx.unit );
|
163 | 163 | };
|
164 |
| -</pre> |
| 164 | +</code></pre> |
165 | 165 | <p>Note that this works best for simple numeric-value animations. More custom code may be required depending on the CSS property, the type of value it returns, and the animation's complexity.</p>
|
166 | 166 | </longdesc>
|
167 | 167 | <category slug="css"/>
|
|
0 commit comments