YUI Library Examples: Animation Utility: Animation Easing

Animation Utility: Animation Easing

This demonstrates how to apply an Easing to a YUI Animation instance. Click the button to begin the demo.

Demo

Animation with Easing

The YUI Animation Utility includes an Easing feature, which allows you to customize how the animation behaves.

For this example, we will animate the width of the <div> element named demo.

Add a little style so that we can see the animation in action:

1<style type="text/css"
2#demo { 
3    background:#ccc; 
4    overflow:hidden; /* so we can animate to zero width */ 
5    width:200px; 
6
7</style> 
view plain | print | ?

Create the demo element and a button to run the animation:

1<div id="demo">Demo</div> 
2<button id="demo-run">run</button> 
view plain | print | ?

Now we create an instance of YAHOO.util.Anim, passing it the element we wish to animate, and the style attribute(s) to be animated. The duration and easing arguments are optional. In this case, we will use 1 second as the duration, and the easeOut easing, which will slow the animation gradually as it nears the end:

1<script type="text/javascript"
2    var attributes = { 
3        width: { to: 0 } 
4    }; 
5    var anim = new YAHOO.util.Anim('demo', attributes, 1, YAHOO.util.Easing.easeOut); 
6</script> 
view plain | print | ?

The final step is to call the animate method on our instance to start the animation. The button will be the trigger that begins the animation sequence:

1<script type="text/javascript"
2YAHOO.util.Event.on('demo-run''click'function() { 
3    anim.animate(); 
4}); 
5</script> 
view plain | print | ?

This is an example of the Easing feature of the YUI Animation Utility.

Copyright © 2008 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings