02.06.2013 Views

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

[ 177 ]<br />

Chapter 6<br />

4. Run the page in a browser and again, click the without entering anything<br />

into the text field. The should transition to the error class and appear<br />

the same as it did in the last example. This time however enter some text in the<br />

and click the again. The error should then transition back<br />

to default.<br />

What just happened?<br />

This time if the has no value we just call the switchClass() method specifying<br />

the current class <strong>of</strong> default, the new class <strong>of</strong> error, and a duration <strong>of</strong> 1.2 seconds. Note<br />

that you must supply both the current and new classes for the example to work correctly.<br />

In the next branch <strong>of</strong> the conditional, we check that the has both a value and a<br />

class name <strong>of</strong> error. If it does we call the removeClass() method specifying just the<br />

class to remove and a duration. The duration is required in order to trigger the transition.<br />

In the CSS we provide the default styling using the class name default as well as generally<br />

for all input elements. We need to do this because otherwise the element loses its styles<br />

while the error class is in the process <strong>of</strong> being removed, causing it to revert to a standard<br />

un-styled element.<br />

Performance<br />

When using <strong>jQuery</strong>, we are always advised that changing the class name <strong>of</strong><br />

an element is more efficient than manipulating an element's style attribute<br />

directly, so it's natural to assume that using switchClass() would be more<br />

efficient than using animate().<br />

This however is not the case, as Firebug's pr<strong>of</strong>ile tool will show. In the previous<br />

example, if the second branch <strong>of</strong> the conditional is removed and the page<br />

and both color-animation.html and class-animation.html are<br />

pr<strong>of</strong>iled, it is color-animation.html that wins by a margin <strong>of</strong> around 20<br />

milliseconds.<br />

Pop quiz – easing, color, and class animations<br />

1. How are easing functions specified?<br />

a. In string format as the third argument to the effect() method, for example<br />

effect("blind", {}, "easeOutBounce")<br />

b. As Boolean in a callback function, for example effect("blind",<br />

function() { easeOutBounce = true })<br />

c. Easing cannot be used<br />

d. In string format as the value <strong>of</strong> the easing configuration option, for example<br />

effect("blind", { easing: "easeOutBounce" })

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!