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

[ 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 ]

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 ]

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

Saved successfully!

Ooh no, something went wrong!