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

[ 141 ] Chapter 6 Optionally, a callback function may be provided as the final argument. The supplied function will be executed once for each selected element when the effect ends. Let's look at a few examples of how the effect() method can be used. The bounce effect The bounce effect is similar to, but much more controllable than, the easeOutBounce easing function. It can be used with either the effect API or show/hide logic depending on your requirements. Configuration options The following configuration options are available for the bounce effect: Option Default Usage direction "up" The direction of bounce. The other possible option is the string down. distance 20 The initial distance of bounce (successive bounces reduce in distance) in pixels. mode "effect" Whether to run the effect normally or use show/hide logic. Other values accepted may be the strings show, hide, or toggle. times 5 The number of bounces. Time for action – using the bounce effect In this example we'll see how the jQuery UI effect can be combined to create a bouncing ball that travels across the page: 1. Use the following simple elements in the of the template file: 2. All we need is a simple container and an inner . In the empty function at the end of the , add the following script: $("#ball").click(function() { $("#travel").animate({ left: "+=300px" }, 2000).find("div").effect("bounce"); });

Extended Animations with jQuery UI 3. Save the file as bounce.html. We also need a few simple styles. Add the following CSS to a new file: #travel { position:absolute; top:100px; } #ball { width:150px; height:150px; cursor:pointer; background:url(../img/ball.jpg) no-repeat 0 0; } 4. Save this as bounce.css in the css folder. When we run the page and click on the ball we should find that it bounces along the page, gradually coming to a halt: The previous composition shows the ball traveling across the page, bouncing up and down as it goes from left to right. What just happened? When the ball is clicked, we first use jQuery's animate() method to animate the left style property of the container by 300 pixels, over a duration of 2 seconds. We slow this animation down to improve the appearance of the overall animation, but it is not strictly required. We then navigate down to the inner element and use the effect() method, specifying the bounce effect. We need to use both elements because if we use the animate() and effect() methods on the same element, the bounce effect will go into the element's animation queue and the two animations will execute one after the other instead of running simultaneously. [ 142 ]

Extended <strong>Animation</strong>s with <strong>jQuery</strong> UI<br />

3. Save the file as bounce.html. We also need a few simple styles. Add the following<br />

CSS to a new file:<br />

#travel { position:absolute; top:100px; }<br />

#ball {<br />

width:150px; height:150px; cursor:pointer;<br />

background:url(../img/ball.jpg) no-repeat 0 0;<br />

}<br />

4. Save this as bounce.css in the css folder. When we run the page and click on the<br />

ball we should find that it bounces along the page, gradually coming to a halt:<br />

The previous composition shows the ball traveling across the page, bouncing up and down as<br />

it goes from left to right.<br />

What just happened?<br />

When the ball is clicked, we first use <strong>jQuery</strong>'s animate() method to animate the left style<br />

property <strong>of</strong> the container by 300 pixels, over a duration <strong>of</strong> 2 seconds. We slow this animation<br />

down to improve the appearance <strong>of</strong> the overall animation, but it is not strictly required. We<br />

then navigate down to the inner element and use the effect() method, specifying<br />

the bounce effect.<br />

We need to use both elements because if we use the animate() and effect() methods<br />

on the same element, the bounce effect will go into the element's animation queue and the<br />

two animations will execute one after the other instead <strong>of</strong> running simultaneously.<br />

[ 142 ]

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

Saved successfully!

Ooh no, something went wrong!