jQuery 1.4 Animation Techniques - Index of
jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of
[ 213 ] Chapter 8 In the previous screenshot, we can see that the proximity container is resized and the scroller element is centered within it. We can also see the default message at the bottom of the proximity container. Time for action – animating the scroller The next section of code deals with actually animating the scroller element based on where the mouse pointer is relative to the outer proximity container: function goAnim(e) { var offset = prox.offset(), resetOffset = e.pageX - offset.left - middle, } normalizedDuration = (resetOffset > 0) ? resetOffset : -resetOffset, duration = (middle - normalizedDuration) * 50; scroller.stop().animate({ left: (resetOffset < 0) ? 0 : "-" + (parseInt(scroller.width()) - parseInt(prox.width())) }, duration, "linear");
Other Popular Animations What just happened? Within the goAnim() function, we first get the offset of the proximity container so that we know its position relative to the document. We then work out where the mouse pointer is relative to the middle of the proximity container. This means that numerically, the pointer offset will be 0 when it is in the center. If the mouse pointer is in the left half of the proximity container, the number in the resetOffset variable will be negative. This would cause our calculations later in the function to be incorrect, so we need to check whether the resetOffset variable is greater than 0, and if it isn't we invert the number using its minus value. Ultimately, what we want to happen is for the speed of the scroller to increase as the pointer moves towards either end of the proximity container, and slow down as it moves into the center. In other words, the speed of the animation needs to be inversely proportionate to the distance of the pointer from the middle of the proximity container. The problem that we have at this point is that the figure representing the distance of the pointer from the middle of the proximity container gets larger as it moves towards the edge, so the animation would slow down instead of speeding up if we were to use this figure as the duration of the animation. To invert the value stored in the normalizedDuration variable, we subtract it from the value representing the middle of the proximity container, and then multiply the resulting figure by 50. The duration argument is in milliseconds, so if we don't use a multiplier (50 was arrived at by trial and error) to increase our value, the animations will occur too quickly. We can now initiate the animation. We use the JavaScript ternary statement to test whether the resetOffset figure is less than 0 and if it is, we know that to get the scroller to slide to the right we just need to set the left style property of the scroller element to 0. If the variable is greater than 0, we have to move the scroller element negatively (to the left) in order to show the images hidden at the right. To align the right edge of the scroller to the right edge of the proximity container, we set the end point of the animation to the width of the scroller minus the width of the proximity container. Time for action – adding the mouse events Now we need to add the mouse events that will trigger the animations: prox.mouseenter(function(e) { message.text(pointerText).delay(1000).fadeOut("slow"); goAnim(e); [ 214 ]
- Page 183 and 184: Extended Animations with jQuery UI
- Page 185 and 186: Extended Animations with jQuery UI
- Page 187 and 188: Extended Animations with jQuery UI
- Page 189 and 190: Extended Animations with jQuery UI
- Page 191 and 192: Extended Animations with jQuery UI
- Page 193 and 194: Extended Animations with jQuery UI
- Page 195 and 196: Extended Animations with jQuery UI
- Page 197 and 198: Extended Animations with jQuery UI
- Page 199 and 200: Extended Animations with jQuery UI
- Page 201 and 202: Full Page Animations Can we animate
- Page 203 and 204: Full Page Animations display:block;
- Page 205 and 206: Download from Wow! eBook Full Page
- Page 207 and 208: Full Page Animations Have a go hero
- Page 209 and 210: Full Page Animations 4. Save this f
- Page 211 and 212: Full Page Animations 2. The bgpos c
- Page 213 and 214: Full Page Animations nav ul { margi
- Page 215 and 216: Full Page Animations screensize = {
- Page 217 and 218: Full Page Animations What just happ
- Page 219 and 220: Full Page Animations The second sec
- Page 221 and 222: Full Page Animations Pop quiz - cre
- Page 223 and 224: Full Page Animations Time for actio
- Page 225 and 226: Full Page Animations Next we define
- Page 228 and 229: 8 Other Popular Animations This cha
- Page 230 and 231: [ 209 ] Chapter 8 } .slider #scroll
- Page 232 and 233: }), pointerText = "Use your pointer
- Page 236 and 237: prox.mousemove(function(ev) { goAni
- Page 238 and 239: Pop quiz - implementing proximity a
- Page 240 and 241: [ 219 ] Chapter 8 What just happene
- Page 242 and 243: 2. Save the new page as marquee.htm
- Page 244 and 245: [ 223 ] Chapter 8 1. jQuery provide
- Page 246 and 247: clear:both; } div#fadeLeft, div#fad
- Page 248 and 249: Time for action - animating the pos
- Page 250: 2. In this example we used the dele
- Page 253 and 254: CSS3 Animations The following trans
- Page 255 and 256: CSS3 Animations Units It is of crit
- Page 257 and 258: CSS3 Animations Skew Remember the t
- Page 259 and 260: CSS3 Animations Calculating the sin
- Page 261 and 262: CSS3 Animations The filter property
- Page 263 and 264: CSS3 Animations If, like me, you th
- Page 265 and 266: CSS3 Animations } el.filters.item("
- Page 267 and 268: CSS3 Animations An unfortunate prob
- Page 269 and 270: CSS3 Animations The context of this
- Page 271 and 272: CSS3 Animations Time for action - i
- Page 273 and 274: CSS3 Animations The first variable
- Page 275 and 276: CSS3 Animations Time for action - a
- Page 277 and 278: Download from Wow! eBook CSS3 Anim
- Page 279 and 280: CSS3 Animations We also have to upd
- Page 281 and 282: CSS3 Animations flatMatrix[3] = fla
- Page 283 and 284: CSS3 Animations When updating the s
Other Popular <strong>Animation</strong>s<br />
What just happened?<br />
Within the goAnim() function, we first get the <strong>of</strong>fset <strong>of</strong> the proximity container so that<br />
we know its position relative to the document. We then work out where the mouse pointer<br />
is relative to the middle <strong>of</strong> the proximity container. This means that numerically, the pointer<br />
<strong>of</strong>fset will be 0 when it is in the center.<br />
If the mouse pointer is in the left half <strong>of</strong> the proximity container, the number in the<br />
resetOffset variable will be negative. This would cause our calculations later in the<br />
function to be incorrect, so we need to check whether the resetOffset variable is greater<br />
than 0, and if it isn't we invert the number using its minus value.<br />
Ultimately, what we want to happen is for the speed <strong>of</strong> the scroller to increase as the pointer<br />
moves towards either end <strong>of</strong> the proximity container, and slow down as it moves into the<br />
center. In other words, the speed <strong>of</strong> the animation needs to be inversely proportionate to<br />
the distance <strong>of</strong> the pointer from the middle <strong>of</strong> the proximity container.<br />
The problem that we have at this point is that the figure representing the distance <strong>of</strong> the<br />
pointer from the middle <strong>of</strong> the proximity container gets larger as it moves towards the edge,<br />
so the animation would slow down instead <strong>of</strong> speeding up if we were to use this figure as<br />
the duration <strong>of</strong> the animation.<br />
To invert the value stored in the normalizedDuration variable, we subtract it from the<br />
value representing the middle <strong>of</strong> the proximity container, and then multiply the resulting<br />
figure by 50. The duration argument is in milliseconds, so if we don't use a multiplier (50 was<br />
arrived at by trial and error) to increase our value, the animations will occur too quickly.<br />
We can now initiate the animation. We use the JavaScript ternary statement to test whether<br />
the resetOffset figure is less than 0 and if it is, we know that to get the scroller to slide to<br />
the right we just need to set the left style property <strong>of</strong> the scroller element to 0.<br />
If the variable is greater than 0, we have to move the scroller element negatively (to the left)<br />
in order to show the images hidden at the right. To align the right edge <strong>of</strong> the scroller <br />
to the right edge <strong>of</strong> the proximity container, we set the end point <strong>of</strong> the animation to the<br />
width <strong>of</strong> the scroller minus the width <strong>of</strong> the proximity container.<br />
Time for action – adding the mouse events<br />
Now we need to add the mouse events that will trigger the animations:<br />
prox.mouseenter(function(e) {<br />
message.text(pointerText).delay(1000).fadeOut("slow");<br />
goAnim(e);<br />
[ 214 ]