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.

Fading <strong>Animation</strong>s<br />

Next we pass a reference to the deleteRow function that we just defined to <strong>jQuery</strong>'s<br />

click() event-helper method. We don't provide the deleteRow function itself as an<br />

argument to the click() method this time, as it is easier and less repetitive to pass the<br />

function reference to the methods instead <strong>of</strong> defining it several times.<br />

We then add a click handling function to the and the elements that exist<br />

in the confirmation panel and are inserted each time a delete icon is clicked. We use the<br />

live() method here so that we don't have to rebind to each handler whenever one <strong>of</strong><br />

these elements are created.<br />

Whether the or the Cancel link is clicked, we first check the id <strong>of</strong> the clicked<br />

element (accessible via the this keyword) to determine which element was clicked.<br />

Because all we are checking is the id <strong>of</strong> the element, we don't need to wrap the this<br />

keyword in <strong>jQuery</strong> functionality ($()).If the id is delete, we know the button was clicked<br />

and can proceed with fading out the so that it is completely transparent, and then<br />

removing it from the page altogether using a callback function supplied as an argument to<br />

the fadeTo() method.<br />

If the id is cancel, we know that the element was clicked. In this case, we stop the<br />

browser from following the link with preventDefault(), remove the pre-delete class<br />

name from the , and then fade it back to full opacity.<br />

We also use a callback function for this method too. Within it we check whether the element<br />

that was faded contains a filter style property, and if it does, we remove the filter<br />

attribute from the element. This fixes the issue with aliased text in IE which affects elements<br />

after they have been faded.<br />

This is all we need to make our fadeTo() example work as intended in most browsers.<br />

Whenever one <strong>of</strong> the delete icons is clicked, the corresponding row is animated to 50%<br />

opacity and the confirmation is then displayed.<br />

This requires the action be confirmed or canceled. When the Cancel link is clicked, the row<br />

is animated back to full opacity. When the delete is clicked, the row is animated<br />

all the way to full transparency and then removed from the page.<br />

Pop quiz – using fadeTo<br />

1. Which arguments must be provided when using the fadeTo() method?<br />

a. The duration and easing arguments<br />

b. The ending opacity<br />

c. A callback function<br />

d. The duration and ending opacity<br />

[ 42 ]

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

Saved successfully!

Ooh no, something went wrong!