02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

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.

CHAPTER 2 ■ COMMON JQUERY ACTIONS AND METHODS<br />

Alternatively, .fadeTo() allows you to specify the opacity to which the element should fade. This<br />

method requires two arguments: a duration <strong>and</strong> the opacity to which the element show fade (a number<br />

between 0 <strong>and</strong> 1). An optional callback can be passed as the third argument as well.<br />

Fade the form to 50 percent opacity <strong>and</strong> log a message using the following:<br />

$("form")<br />

.fadeTo(1000, 0.5, function(){<br />

console.log("Faded to 50%!");<br />

});<br />

.slideUp(), .slideDown(), <strong>and</strong> .slideToggle()<br />

To hide an element <strong>by</strong> reducing its height to 0, .slideUp() is a shortcut method. It animates the<br />

reduction of the element’s height until it reaches 0 <strong>and</strong> then sets display:none; to ensure the layout is<br />

no longer affected <strong>by</strong> the element. To reverse this, the .slideDown() method removes the display:none;<br />

<strong>and</strong> animates the height from 0 back to the original height of the element.<br />

Just like .fadeIn() <strong>and</strong> .fadeOut(), two optional parameters are accepted: the duration <strong>and</strong> a<br />

callback function.<br />

Slide up the paragraph with class foo, log a message, slide it back down, <strong>and</strong> log another message:<br />

$("p.foo")<br />

.slideUp(1000, function(){<br />

console.log("Hidden!");<br />

})<br />

.slideDown(1000, function(){<br />

console.log("Shown!");<br />

});<br />

The .slideToggle() method does the same thing as .slideUp() <strong>and</strong> .slideDown(), but it’s smart<br />

enough to know if an element is hidden or shown <strong>and</strong> uses that information to determine which action<br />

to take.<br />

To set up a display toggle for the paragraph with class foo, use the following:<br />

$("p.foo")<br />

.slideToggle("slow", function(){<br />

console.log("Toggled!");<br />

});<br />

By running this code multiple times, you’ll see the paragraph slide up <strong>and</strong> down in alternating<br />

fashion.<br />

.animate()<br />

The previously discussed animation methods are shortcuts that all call the .animate() method. This<br />

method will animate most visual CSS properties of an element <strong>and</strong> supports easing, which is one of any<br />

number of mathematical formulas that alter the way the animation operates. By default, "linear" <strong>and</strong><br />

"swing" easing are supported, but an easy-to-include easing plug-in is available for <strong>jQuery</strong> (you’ll learn<br />

about plug-ins later in this book).<br />

67

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

Saved successfully!

Ooh no, something went wrong!