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.

Extended <strong>Animation</strong>s with <strong>jQuery</strong> UI<br />

Class transitions<br />

As well as extending <strong>jQuery</strong>'s animate() method in order to provide color animations,<br />

<strong>jQuery</strong> UI also extends some <strong>of</strong> <strong>jQuery</strong>'s element manipulation methods. The following<br />

methods are extended to provide class transitions:<br />

addClass()<br />

removeClass()<br />

toggleClass()<br />

<strong>jQuery</strong> UI also exposes a new method for transitioning between two classes—the<br />

switchClass() method, which accepts the current class and new class, as well<br />

as duration, easing, and callback arguments.<br />

Time for action – transitioning between classes<br />

We can rework our previous example so that it uses some <strong>of</strong> the class transition methods.<br />

1. Add the class name default to the element and then change the<br />

JavaScript so that it appears as follows:<br />

$("#search").click(function(e) {<br />

e.preventDefault();<br />

var input = $(this).prev();<br />

if (input.val() == "") {<br />

input.switchClass("default", "error", 1200);<br />

} else if (input.val() && input.hasClass("error")) {<br />

input.removeClass("error", 1200);<br />

}<br />

});<br />

2. Save the new page as class-animation.html. We'll need to make some changes<br />

to the stylesheet as well. Create a new stylesheet and add the following rules to it<br />

(or change the styles in the <strong>of</strong> the page):<br />

input { width:200px; }<br />

.default, input { border:2px solid #27659f; }<br />

.error { border:2px solid #a72b2e; background-color:#f78080; }<br />

3. Save the new file as class-animation.css.<br />

[ 176 ]

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

Saved successfully!

Ooh no, something went wrong!