Yahoo! Developer Network Home - Help

YDN Site Search

# YUI Library Examples: Animation Utility: Using Custom Units for an Animation

# Animation Utility: Using Custom Units for an Animation

This demonstrates how to use YUI Animation with units other than pixels. Click the button to begin the demo.

View example in new window.

Demo

## Animating in other Units

The YUI Animation Utility allows you to animate style attributes in any CSS unit.

For this example, we will animate the width of the `<div>` element named `demo` in EM units. Using EMs is nice, because it becomes relative to the text size, and will scale as the user changes the browser text size setting.

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

<style type="text/css"> #demo { background:#ccc; width:30em; } </style>

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

<div id="demo">Demo</div> <button id="demo-run">run</button>

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. Because we are changing the unit to something other than the default ("px"), the `from` field is required:

<script type="text/javascript"> var attributes = { width: { from: 30, to: 10, unit:'em' } }; var anim = new YAHOO.util.Anim('demo', attributes); </script>

The final step is to call the `animate` method on our instance to start the animation. To keep things simple, we will animate when the document is clicked:

<script type="text/javascript"> YAHOO.util.Event.on('demo-run', 'click', function() { anim.animate(); }); </script>

This is an example of animating in units other than pixels.

### Animation Utility Examples:

- Basic Animation
- Animation Easing
- Animating From a Given Value
- Using Custom Units for an Animation
- Animating Multiple Attributes
- Chaining Animations Using `onComplete`
- Animating Colors
- Animating Motion
- Animating Along a Curved Path
- Animated Scrolling
- Glowing Button (included with examples for Button Control)
- Complex Application (included with examples for Layout Manager (beta))

### More Animation Utility Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2008 Yahoo! Inc. All rights reserved.

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