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.

[ 175 ]<br />

Chapter 6<br />

2. To invoke the color changes when the is clicked, we can use the following<br />

JavaScript in the empty function near the bottom <strong>of</strong> the document:<br />

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

e.preventDefault();<br />

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

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

input.animate({<br />

backgroundColor: "#f78080",<br />

borderTopColor: "#a72b2e",<br />

borderRightColor: "#a72b2e",<br />

borderBottomColor: "#a72b2e",<br />

borderLeftColor: "#a72b2e"<br />

}, 1200);<br />

};<br />

});<br />

3. Save this page as color-animations.html. We literally only need a couple<br />

<strong>of</strong> styles for this example. We could probably get away with defining them in a<br />

block in the <strong>of</strong> the page. We just use the following CSS:<br />

input { width:200px; border:2px solid #27659f; }<br />

4. When we run the page, we see that the text field changes color if the is<br />

clicked while it is empty.<br />

What just happened?<br />

The CSS, while extremely small, is required in this example because the will lose<br />

any attractive styling provided by modern browsers when the colors are animated. Setting<br />

the CSS properties we are animating helps prevent this ugly switch.<br />

In the script we simply cache a selector that points to the , and then test whether<br />

the field is empty. If it is we call the animate() method, specifying the aspects <strong>of</strong> the target<br />

element we'd like to animate. Notice that we must specify each border-color independently<br />

for the animation to work correctly.

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

Saved successfully!

Ooh no, something went wrong!