jQuery 1.4 Animation Techniques - Index of
jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of
Download from Wow! eBook }; $("#go").toggle(function() { loader.show(); loadingInterval = setInterval(function() { runLoader(); }, 1200); }, function() { loader.hide(); clearInterval(loadingInterval); }); [ 15 ] Chapter 1 3. Save the file as loading.html in the main project folder (jquery-animation). Finally, we'll need to add a few basic styles to the example. Create a new file in your text editor and add to it the following code: #loader { margin:10px 0 0 36px; } #loader span { display:block; width:6px; float:left; margin-right:6px; border:1px solid #336633; position:relative; background-color:#ccffcc; } #loader .bar-0 { height:15px; bottom:-20px; } #loader .bar-1 { height:25px; bottom:-10px; } #loader .bar-2 { height:35px; margin-right:0; } 4. Save this file in the css folder as loading.css. What just happened? The hardcoded onto the page is used to show and hide the loading animation. This is done purely for the purpose of this example. In an actual implementation, we'd show the loading animation at the start of a load operation, when new content was being added to the page for example, and then hide it again once the operation was complete. The first thing we do inside the outer function is set some variables. We create a new element as a container for the loader, using an object literal as the second argument to the $() (jQuery()) method to give it an id of loader. We then set its style to display:none with jQuery's css() method so that it is not immediately visible. We also create a new element, which will be used as a template to create the three individual loading bars. We set its opacity to 0.2 (20% opaque), also using the css() method. jQuery normalizes this style for us so that it works correctly in Internet Explorer. The last variable, loadingInterval will be used to store the id of an interval so that we can clear the interval when we need to. We set this to null initially as the interval has not yet been set.
Introduction Once our variables have been defined and initialized, we then execute a short for loop, with just three iterations. Within this loop we clone the span element we created, give it a class name for styling purposes, and then append it to the container. Once the three loading bars have been added to the container, we insert the container after the . Next we define a function called runLoader. This is the function that will be repeatedly called by the interval. The function doesn't run until the button is clicked. Within this function we cache the selector for each of the three individual bars and then run a series of nested functions. We first increase the first loading bar to full opacity using the fadeTo() jQuery animation method. This method takes a string indicating the speed of the animation as its first argument, the opacity that the element should be faded to as its second argument, and a callback function as the third argument. The callback function is executed as soon as the animation ends. In the callback function, we then fade the first loading bar back to its original opacity of 0.2. We supply another callback function to this method call, and within this callback function we animate the second loading bar to full opacity, and then back to its original opacity. The process is repeated for the third loading bar. Finally, we use the jQuery toggle() method to add two functions which will be executed alternately each time the is clicked. In the first function, we show the loader and then set the interval that repeatedly calls the runLoader() function. In the second function, we hide the loader and clear the interval. Pop quiz – basic animation with jQuery 1. Thinking about what we discussed earlier regarding when and when not to use animations, when would be an appropriate time to use this animation? a. When there is a browser-intensive operation taking place b. When there is a delay between something being requested from the server and the request returning from the server, but where the processing required by the browser is minimal c. As an alternative to a Flash animation d. When animated GIF images are not supported 2. What arguments are used with jQuery's fadeTo() method? a. An integer representing the ending opacity b. An object containing configuration options for the animation [ 16 ]
- Page 2 and 3: Download from Wow! eBook jQuery 1.
- Page 4 and 5: Author Dan Wellman Reviewers Shaifu
- Page 6 and 7: About the Author Dan Wellman is an
- Page 8 and 9: Cyril Pierron is an engineer, a web
- 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: Introduction In the previous screen
- 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 62 and 63: [ 41 ] Chapter 2 What just happened
- 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
Introduction<br />
Once our variables have been defined and initialized, we then execute a short for loop, with<br />
just three iterations. Within this loop we clone the span element we created, give it a class<br />
name for styling purposes, and then append it to the container. Once the three loading bars<br />
have been added to the container, we insert the container after the .<br />
Next we define a function called runLoader. This is the function that will be repeatedly<br />
called by the interval. The function doesn't run until the button is clicked. Within this<br />
function we cache the selector for each <strong>of</strong> the three individual bars and then run a series<br />
<strong>of</strong> nested functions.<br />
We first increase the first loading bar to full opacity using the fadeTo() <strong>jQuery</strong> animation<br />
method. This method takes a string indicating the speed <strong>of</strong> the animation as its first<br />
argument, the opacity that the element should be faded to as its second argument, and a<br />
callback function as the third argument. The callback function is executed as soon as the<br />
animation ends.<br />
In the callback function, we then fade the first loading bar back to its original opacity <strong>of</strong> 0.2.<br />
We supply another callback function to this method call, and within this callback function<br />
we animate the second loading bar to full opacity, and then back to its original opacity. The<br />
process is repeated for the third loading bar.<br />
Finally, we use the <strong>jQuery</strong> toggle() method to add two functions which will be executed<br />
alternately each time the is clicked. In the first function, we show the loader<br />
and then set the interval that repeatedly calls the runLoader() function. In the second<br />
function, we hide the loader and clear the interval.<br />
Pop quiz – basic animation with <strong>jQuery</strong><br />
1. Thinking about what we discussed earlier regarding when and when not to use<br />
animations, when would be an appropriate time to use this animation?<br />
a. When there is a browser-intensive operation taking place<br />
b. When there is a delay between something being requested from the server<br />
and the request returning from the server, but where the processing required<br />
by the browser is minimal<br />
c. As an alternative to a Flash animation<br />
d. When animated GIF images are not supported<br />
2. What arguments are used with <strong>jQuery</strong>'s fadeTo() method?<br />
a. An integer representing the ending opacity<br />
b. An object containing configuration options for the animation<br />
[ 16 ]