jQuery 1.4 Animation Techniques - Index of
jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of
[ 87 ] Chapter 4 The previous screenshots shows the animation in a top-down format with the start of the animation at the top and the animation nearing the end at the bottom. As we're using the slideToggle() method, it is equally valid when running backwards too.
Sliding Animations What just happened? The underlying structures we've placed on the page are pretty straight-forward. To summarize, we basically have two tables, one of which is wrapped in a , and an outer container for the whole collection of elements. The table that is wrapped will be hidden when the basket is collapsed. We also have a link at the top of the container which will show or hide the basket contents. Our JavaScript code is divided into two main sections. In the first section, we cache some jQuery selectors that we'll use a couple of times in the code to save from selecting them one at a time, each time they are required. We also define a function that we can call whenever we need to change the value of the toggling link. The function sets the text of the link using an anonymous function which returns the new text to add. This is passed to the index of the element in the collection on which the text() method was called as the first argument and the second is the original text of the element. We don't need the first argument but we must specify it in order to access the second argument. The second part of the code is the click handler for the toggle link. It checks that the containing the first is not already being animated and if not, it calls the slideToggle() method, which will either slide the basket open or closed depending on its current state. We use the callback function to update the text of the link once the animation is complete. The: animated filter Checking whether an element is already being animated using the :animated filter is a quick and easy alternative to the stop() method in the previous example in that it only applied the animation if the element was not already animated and therefore prevented a build-up of animations. Have a go hero – doing more with slideToggle Have a go at reworking the code so that it uses the slideDown() and slideUp() methods instead of the slideToggle(). It won't make the code any more efficient, but you should get to see roughly how much code the slideToggle() method can save you. [ 88 ]
- 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
- 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 110 and 111: Pop quiz - using slideToggle 1. Wha
- Page 112 and 113: [ 91 ] Chapter 4 Easing is a great
- Page 114 and 115: [ 93 ] Chapter 4 Home Articles J
- Page 116 and 117: [ 95 ] Chapter 4 3. Save this file
- Page 118 and 119: In the previous screenshot, we see
- Page 120 and 121: Pop quiz - fixing the flicker 1. Wh
- Page 122 and 123: 5 Custom Animations The predefined
- Page 124 and 125: An alternative syntax for animate()
- Page 126 and 127: [ 105 ] Chapter 5 width:295px; marg
- Page 128 and 129: [ 107 ] Chapter 5 What just happene
- Page 130 and 131: [ 109 ] Chapter 5 Although not esse
- Page 132 and 133: [ 111 ] Chapter 5 The previous scre
- Page 134 and 135: [ 113 ] Chapter 5 The styles we use
- Page 136 and 137: [ 115 ] Chapter 5 What just happene
- Page 138 and 139: }, html: copy, width: expander.widt
- Page 140 and 141: [ 119 ] Chapter 5 If the image alre
- Page 142 and 143: [ 121 ] Chapter 5 d. Two objects wh
- Page 144 and 145: [ 123 ] Chapter 5 text-align:center
- Page 146 and 147: [ 125 ] Chapter 5 What just happene
- Page 148 and 149: [ 127 ] Chapter 5 Finally, we retur
- Page 150 and 151: [ 129 ] Chapter 5 Once we've define
- Page 152 and 153: } }; $(config.selector).find("a").c
- Page 154 and 155: [ 133 ] Chapter 5 In the previous s
- Page 156: Have a go hero - extending the plug
Sliding <strong>Animation</strong>s<br />
What just happened?<br />
The underlying structures we've placed on the page are pretty straight-forward. To<br />
summarize, we basically have two tables, one <strong>of</strong> which is wrapped in a , and an outer<br />
container for the whole collection <strong>of</strong> elements. The table that is wrapped will be hidden<br />
when the basket is collapsed. We also have a link at the top <strong>of</strong> the container which will show<br />
or hide the basket contents.<br />
Our JavaScript code is divided into two main sections. In the first section, we cache some<br />
<strong>jQuery</strong> selectors that we'll use a couple <strong>of</strong> times in the code to save from selecting them one<br />
at a time, each time they are required.<br />
We also define a function that we can call whenever we need to change the value <strong>of</strong> the<br />
toggling link. The function sets the text <strong>of</strong> the link using an anonymous function which<br />
returns the new text to add. This is passed to the index <strong>of</strong> the element in the collection on<br />
which the text() method was called as the first argument and the second is the original<br />
text <strong>of</strong> the element. We don't need the first argument but we must specify it in order to<br />
access the second argument.<br />
The second part <strong>of</strong> the code is the click handler for the toggle link. It checks that the<br />
containing the first is not already being animated and if not, it calls the<br />
slideToggle() method, which will either slide the basket open or closed depending on its<br />
current state. We use the callback function to update the text <strong>of</strong> the link once the animation<br />
is complete.<br />
The: animated filter<br />
Checking whether an element is already being animated using the :animated<br />
filter is a quick and easy alternative to the stop() method in the previous<br />
example in that it only applied the animation if the element was not already<br />
animated and therefore prevented a build-up <strong>of</strong> animations.<br />
Have a go hero – doing more with slideToggle<br />
Have a go at reworking the code so that it uses the slideDown() and slideUp() methods<br />
instead <strong>of</strong> the slideToggle(). It won't make the code any more efficient, but you should<br />
get to see roughly how much code the slideToggle() method can save you.<br />
[ 88 ]