Yahoo! Developer Network Home - Help

YUI Library Examples: Animation Utility: Animating Colors

Animation Utility: Animating Colors

This demonstrates how to use the YUI Animation to animate colors. Click the button to begin the demo.

Demo

Animating Colors

The YUI Animation Utility allows you to animate the colors of an HTMLElement.

For this example, we will animate the color (text) and background of a <div> element named demo.

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

1<style type="text/css"
2#demo { 
3    background:#ccc; 
4    height:300px; 
5    width:300px; 
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:

1<script type="text/javascript"
2    var attributes = { 
3        color: { to: '#06e' }, 
4        backgroundColor: { to: '#e06' } 
5    }; 
6    var anim = new YAHOO.util.ColorAnim('demo', attributes); 
7</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 animating multiple style attributes of an HTMLElement.

Copyright © 2008 Yahoo! Inc. All rights reserved.

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