jQuery 1.4 Animation Techniques - Index of
jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of
[ 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 ]
- Page 12: For Pat Spacagna, the greatest moth
- Page 15 and 16: Download from Wow! eBook Table of
- Page 17 and 18: Table of Contents Time for action -
- Page 19 and 20: Table of Contents jQuery and transf
- Page 21 and 22: Drawing to the canvas 309 Supportin
- Page 23 and 24: Preface What this book covers Chapt
- Page 25 and 26: Preface Have a go hero - heading Th
- Page 27 and 28: Preface Piracy Piracy of copyright
- Page 29 and 30: Introduction In addition to animate
- Page 31 and 32: Introduction When not to use animat
- Page 33 and 34: Introduction of the template file"
- Page 35 and 36: Introduction In the previous screen
- Page 37 and 38: Introduction Once our variables hav
- Page 40 and 41: 2 Fading Animations In this chapter
- Page 42 and 43: [ 21 ] Chapter 2 We can control the
- Page 44 and 45: The previous screenshot shows how t
- Page 46 and 47: [ 25 ] Chapter 2 Most of the styles
- Page 48 and 49: 2. What else can be passed into the
- Page 50 and 51: Time for action - creating the dial
- Page 52 and 53: [ 31 ] Chapter 2 For example, the a
- Page 54 and 55: Download from Wow! eBook The follo
- Page 56 and 57: [ 35 ] Chapter 2 The CSS is used mo
- Page 58 and 59: The fadeTo() method is used in the
- Page 60 and 61: [ 39 ] Chapter 2 #messageList table
- Page 64 and 65: 2. What format can these arguments
- Page 66 and 67: } var el = $(this); if (el.css("fil
- Page 68 and 69: [ 47 ] Chapter 2 Usually these meth
- Page 70 and 71: Time for action - animations with s
- Page 72 and 73: Download from Wow! eBook [ 51 ] Ch
- Page 74 and 75: 2. Save the changes as toggle.html.
- Page 76 and 77: 3 Managing Animations Like most of
- Page 78 and 79: [ 57 ] Chapter 3 A custom queue tha
- Page 80 and 81: If we wish to see how many function
- Page 82 and 83: 2. What does the queue() method ret
- Page 84 and 85: [ 63 ] Chapter 3 The argument we su
- Page 86 and 87: [ 65 ] Chapter 3 We pass a callback
- Page 88 and 89: [ 67 ] Chapter 3 The first argument
- Page 90 and 91: Delaying queue execution [ 69 ] Cha
- Page 92 and 93: [ 71 ] Chapter 3 Also note that the
- Page 94 and 95: 4 Sliding Animations Another type o
- Page 96 and 97: Time for action - creating a slide-
- Page 98 and 99: [ 77 ] Chapter 4 header form input
- Page 100 and 101: Pop quiz - sliding elements down 1.
- Page 102 and 103: Download from Wow! eBook }); }); $
- Page 104 and 105: Toggling the slide [ 83 ] Chapter 4
- Page 106 and 107: [ 85 ] Chapter 4 } h2 { margin:0; p
- Page 108 and 109: [ 87 ] Chapter 4 The previous scree
- Page 110 and 111: Pop quiz - using slideToggle 1. Wha
[ 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.