This demonstrates how to use the YUI Animation to animate the motion of an HTMLElement. Click the button to begin the demo.
The YUI Animation Utility allows you to animate the motion of an HTMLElement.
For this example, we will animate the position of a <div>
element named demo
. Note that you could accomplish this with a positioned element by animating offset properties ("top", "left", etc.), but those values are not necessarily in page coordinates. The points
attribute, introduced in the YAHOO.util.Motion
subclass, ensures that position is always relative to the document (unless using by
rather than to
, which is relative to the current position).
Add a little style so that we can see the animation in action:
1 | <style type="text/css"> |
2 | #demo { |
3 | background:#ccc; |
4 | height:30px; |
5 | width:30px; |
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.Motion
, passing it the element we wish to animate, and the points attribute (an array of [x, y] positions), with the point we are animating to:
1 | <script type="text/javascript"> |
2 | var attributes = { |
3 | points: { to: [600, 10] } |
4 | }; |
5 | var anim = new YAHOO.util.Motion('demo', attributes); |
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"> |
2 | YAHOO.util.Event.on('demo-run', 'click', function() { |
3 | anim.animate(); |
4 | }); |
5 | </script> |
view plain | print | ? |
This is an example of animating the motion HTMLElement.
You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.
onComplete
Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings