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

Time for action – animating the post links [ 227 ] Chapter 8 We're now ready to begin scrolling the post links within the marquee. We can do this using our custom event. 1. After the getJSON() method, add the following code to the page: $("body").delegate("#marquee", "marquee-ready", function() { var marquee = $(this), postLink = marquee.find("a").eq(0); width = postLink.width() + parseInt(postLink.css("marginRight")), time = 15 * width; postLink.animate({ marginLeft: "-=" + width }, time, "linear", function() { $(this).css({ marginLeft: 0 }).appendTo(marquee.find("div").eq(0)); marquee.trigger("marquee-ready"); }); }); 2. Our example is now complete. When we run the page at this point, the posts should begin scrolling from left to right. What just happened? We use the jQuery delegate() method to bind an event handler to our custom marquee-ready event. We need to use event delegation to achieve this because when this part of the code is executed, the JSON response is unlikely to have returned so the marquee element won't even exist. Attaching the event handler to the body of the page is an easy way to prepare the page for when the marquee element does exist. Within the anonymous event-handling function, we first cache a reference to the marquee element using the this object, which is scoped to our marquee element. We then select the first link in the marquee, and determine its total width including margin. We also work out what is effectively the speed of the animation. jQuery animations use a duration to determine how quickly an animation should run, but the problem this causes us is that posts with longer titles will move faster, because they have a greater distance to animate in the same amount of time.

Other Popular Animations To fix this, we work out a duration to pass to the animation method based on an arbitrary "speed" of 15 multiplied by the width of the current . This ensures that each post will scroll at the same speed regardless of how long it is. Once we have obtained the total width and duration, we can then run the animation on the first link in the marquee, using our width and time variables to configure the animation. We animate the post link by setting a negative margin of the first link, which drags all of the other links along with it. Once the animation is complete, we remove the margin-left from the link, re-append it to the end of the within the marquee element, and fire the marquee-ready event once more to repeat the process. This occurs repeatedly, creating the ongoing animation and bringing us to the end of this example. Have a go hero – extending the marquee scroller One feature that would certainly be beneficial to your users would be if the post titles stopped being animated when the mouse pointer hovered over them. The animation could then be restarted when the pointer moves off them again. Have a go at adding this functionality yourself. It shouldn't be too tricky at all and should involve adding mouseenter and mouseleave event handlers. You'll need to work out how much of any given link is already outside of the visible area of the marquee in order to ensure the animation restarts at the same speed that it stopped at, but this should be quite similar to how we worked out the duration in this version of the example. See how you get on. Pop Quiz – creating a marquee scroller 1. Why did we create a dynamic-duration variable (time) instead of using one of jQuery's predefined durations? a. Because its quicker using an integer, even if that integer has to be calculated, than using one of the duration strings b. Because it's more fun c. To make sure the links are appended to the correct element after being animated d. To ensure that the links all animate at the same speed regardless of how long they are [ 228 ]

Other Popular <strong>Animation</strong>s<br />

To fix this, we work out a duration to pass to the animation method based on an arbitrary<br />

"speed" <strong>of</strong> 15 multiplied by the width <strong>of</strong> the current . This ensures that each post will<br />

scroll at the same speed regardless <strong>of</strong> how long it is.<br />

Once we have obtained the total width and duration, we can then run the animation<br />

on the first link in the marquee, using our width and time variables to configure the<br />

animation. We animate the post link by setting a negative margin <strong>of</strong> the first link, which<br />

drags all <strong>of</strong> the other links along with it.<br />

Once the animation is complete, we remove the margin-left from the link, re-append it<br />

to the end <strong>of</strong> the within the marquee element, and fire the marquee-ready event<br />

once more to repeat the process. This occurs repeatedly, creating the ongoing animation and<br />

bringing us to the end <strong>of</strong> this example.<br />

Have a go hero – extending the marquee scroller<br />

One feature that would certainly be beneficial to your users would be if the post titles<br />

stopped being animated when the mouse pointer hovered over them. The animation<br />

could then be restarted when the pointer moves <strong>of</strong>f them again. Have a go at adding this<br />

functionality yourself. It shouldn't be too tricky at all and should involve adding mouseenter<br />

and mouseleave event handlers.<br />

You'll need to work out how much <strong>of</strong> any given link is already outside <strong>of</strong> the visible area <strong>of</strong><br />

the marquee in order to ensure the animation restarts at the same speed that it stopped<br />

at, but this should be quite similar to how we worked out the duration in this version <strong>of</strong> the<br />

example. See how you get on.<br />

Pop Quiz – creating a marquee scroller<br />

1. Why did we create a dynamic-duration variable (time) instead <strong>of</strong> using one <strong>of</strong><br />

<strong>jQuery</strong>'s predefined durations?<br />

a. Because its quicker using an integer, even if that integer has to be calculated,<br />

than using one <strong>of</strong> the duration strings<br />

b. Because it's more fun<br />

c. To make sure the links are appended to the correct element after being<br />

animated<br />

d. To ensure that the links all animate at the same speed regardless <strong>of</strong> how long<br />

they are<br />

[ 228 ]

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

Saved successfully!

Ooh no, something went wrong!