anim-xy.mustache revision 77b17de8f98cd3600f3361da39ab57166bd3be1b
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<link href="{{componentAssets}}/anim.css" rel="stylesheet" type="text/css">
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<div class="intro">
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney <p>This demonstrates how to animate the <code>xy</code> position of an element.</p>
77b17de8f98cd3600f3361da39ab57166bd3be1bMatt Sweeney <p>Click anywhere on the gray box below and the little orange box will move to the click position.</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney</div>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<div class="example">
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney{{>anim-xy-source}}
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney</div>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<h3>Setting up the HTML</h3>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<p>First we add some HTML to animate.</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney```
70a56a4860f974884bce155389d59e365e18e111Matt Sweeney<span id="demo"></span>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney```
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<h3>Creating the Anim Instance</h3>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<p>Now we create an instance of <code>Y.Anim</code>, passing it a configuration object that includes the <code>node</code> we wish to animate. We will set the <code>to</code> attribute later when then animation runs.</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney```
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeneyvar anim = new Y.Anim({
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney node: '#demo',
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney duration: 0.5,
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney easing: Y.Easing.elasticOut
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney});
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney```
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<h3>Changing Attributes</h3>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<p>Next we need a <code>click</code> handler to set the <code>to</code> attribute for the <code>xy</code> behavior and run the animation.</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney```
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeneyvar onClick = function(e) {
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney anim.set('to', { xy: [e.pageX, e.pageY] });
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney anim.run();
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney};
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney```
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<h3>Running the Animation</h3>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<p>Finally we add an event handler to run the animation when the document is clicked.</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney```
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt SweeneyY.one('document').on('click', onClick);
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney```
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<h2>Complete Example Source</h2>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney```
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney{{>anim-xy-source}}
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney```