jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of

02.06.2013 Views

[ 41 ] Chapter 2 What just happened? Our script is a little longer than those in previous examples, but still relatively simple; let's step through what happens. First we get a reference to the outer container for our message widget. We get the element using the standard JavaScript document.getElementById() function. We can use this DOM node as a context for jQuery methods to make selecting elements from the DOM faster. Selecting elements by class name is not very efficient (even with jQuery), so being able to pass in a DOM node to a jQuery selector to tell jQuery where to begin for searching for the element makes our queries much faster than searching through the entire document each time we want to get an element using its class name. We also store a reference to the element as we'll need to refer to this element throughout the script. Instead of creating a new jQuery object and selecting the element each time we need to manipulate it, we create a single jQuery object representing the element (notice how we use our messageList variable as a context for the selection) and store it in a variable for use as many times as we require with no additional overhead. Next we create a series of new elements for use later in the script. We create a container and give it some attributes including a class name and some text. Note that the word class is surrounded with quotation marks to prevent Internet Explorer throwing script errors. We also create a new element and a new element and give both of these some attributes too. The new button and anchor elements are appended to the new container. These elements are not added to the page however, they are kept in memory for use later on in the script. Next we add an inline function stored in the deleteRow variable, which is used to handle clicks on the delete icons in each row of the . Within this function we first use the preventDefault() JavaScript method to prevent the delete link being "followed" by the browser and jumping back to the top of the page. We then select the closest parent element and call the fadeTo() method specifying a duration of 400 milliseconds (the default) and an ending opacity of 0.5. We also supply a callback function that is executed when the animation ends. We use this function to add a class name to the row so that we can easily refer to it later on. We then create a copy of the container element (including the child elements we added to it) that we created at the start of the script using jQuery's clone() method. Copying these elements from memory is much more efficient than creating them from scratch each time the function is executed. The copy of the container is then inserted into the widget after the . To prevent other messages being selected for deletion (and a build-up of confirmation messages), we unbind the click handler from the delete links in each row. We don't need to select the links from the DOM at this point. We use the messages variable (containing a reference to the ) and jQuery's find() method to select the links without needing to create a new jQuery object.

Fading Animations Next we pass a reference to the deleteRow function that we just defined to jQuery's click() event-helper method. We don't provide the deleteRow function itself as an argument to the click() method this time, as it is easier and less repetitive to pass the function reference to the methods instead of defining it several times. We then add a click handling function to the and the elements that exist in the confirmation panel and are inserted each time a delete icon is clicked. We use the live() method here so that we don't have to rebind to each handler whenever one of these elements are created. Whether the or the Cancel link is clicked, we first check the id of the clicked element (accessible via the this keyword) to determine which element was clicked. Because all we are checking is the id of the element, we don't need to wrap the this keyword in jQuery functionality ($()).If the id is delete, we know the button was clicked and can proceed with fading out the so that it is completely transparent, and then removing it from the page altogether using a callback function supplied as an argument to the fadeTo() method. If the id is cancel, we know that the element was clicked. In this case, we stop the browser from following the link with preventDefault(), remove the pre-delete class name from the , and then fade it back to full opacity. We also use a callback function for this method too. Within it we check whether the element that was faded contains a filter style property, and if it does, we remove the filter attribute from the element. This fixes the issue with aliased text in IE which affects elements after they have been faded. This is all we need to make our fadeTo() example work as intended in most browsers. Whenever one of the delete icons is clicked, the corresponding row is animated to 50% opacity and the confirmation is then displayed. This requires the action be confirmed or canceled. When the Cancel link is clicked, the row is animated back to full opacity. When the delete is clicked, the row is animated all the way to full transparency and then removed from the page. Pop quiz – using fadeTo 1. Which arguments must be provided when using the fadeTo() method? a. The duration and easing arguments b. The ending opacity c. A callback function d. The duration and ending opacity [ 42 ]

[ 41 ]<br />

Chapter 2<br />

What just happened?<br />

Our script is a little longer than those in previous examples, but still relatively simple; let's<br />

step through what happens. First we get a reference to the outer container for our message<br />

widget. We get the element using the standard JavaScript document.getElementById()<br />

function. We can use this DOM node as a context for <strong>jQuery</strong> methods to make selecting<br />

elements from the DOM faster. Selecting elements by class name is not very efficient<br />

(even with <strong>jQuery</strong>), so being able to pass in a DOM node to a <strong>jQuery</strong> selector to tell <strong>jQuery</strong><br />

where to begin for searching for the element makes our queries much faster than searching<br />

through the entire document each time we want to get an element using its class name.<br />

We also store a reference to the element as we'll need to refer to this element<br />

throughout the script. Instead <strong>of</strong> creating a new <strong>jQuery</strong> object and selecting the element<br />

each time we need to manipulate it, we create a single <strong>jQuery</strong> object representing the<br />

element (notice how we use our messageList variable as a context for the selection)<br />

and store it in a variable for use as many times as we require with no additional overhead.<br />

Next we create a series <strong>of</strong> new elements for use later in the script. We create a container<br />

and give it some attributes including a class name and some text. Note that the<br />

word class is surrounded with quotation marks to prevent Internet Explorer throwing<br />

script errors.<br />

We also create a new element and a new element and give both <strong>of</strong> these<br />

some attributes too. The new button and anchor elements are appended to the new<br />

container. These elements are not added to the page however, they are kept in memory<br />

for use later on in the script.<br />

Next we add an inline function stored in the deleteRow variable, which is used to handle<br />

clicks on the delete icons in each row <strong>of</strong> the . Within this function we first use the<br />

preventDefault() JavaScript method to prevent the delete link being "followed" by the<br />

browser and jumping back to the top <strong>of</strong> the page. We then select the closest parent <br />

element and call the fadeTo() method specifying a duration <strong>of</strong> 400 milliseconds<br />

(the default) and an ending opacity <strong>of</strong> 0.5.<br />

We also supply a callback function that is executed when the animation ends. We use this<br />

function to add a class name to the row so that we can easily refer to it later on. We then<br />

create a copy <strong>of</strong> the container element (including the child elements we added to it) that we<br />

created at the start <strong>of</strong> the script using <strong>jQuery</strong>'s clone() method. Copying these elements<br />

from memory is much more efficient than creating them from scratch each time the function<br />

is executed. The copy <strong>of</strong> the container is then inserted into the widget after the .<br />

To prevent other messages being selected for deletion (and a build-up <strong>of</strong> confirmation<br />

messages), we unbind the click handler from the delete links in each row. We don't need<br />

to select the links from the DOM at this point. We use the messages variable (containing a<br />

reference to the ) and <strong>jQuery</strong>'s find() method to select the links without needing<br />

to create a new <strong>jQuery</strong> object.

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

Saved successfully!

Ooh no, something went wrong!