reverse.mustache revision 1f08a8488664773a7d96fa3a043a639692d2a5cb
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<link href="{{componentAssets}}/anim.css" rel="stylesheet" type="text/css">
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<div class="intro">
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney <p>This demonstrates how to use the <code>reverse</code> attribute to change the behavior of the animation.</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney <p> Click the icon in the header to toggle the element's <code>height</code>.</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<div class="example">
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney{{>reverse-source}}
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<h2>Setting up the HTML</h2>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<p>First we add some HTML to animate.</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<div id="demo" class="yui3-module">
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney <div class="yui3-hd">
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney <h3>Animation Demo</h3>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney <a title="remove module" class="yui3-toggle"><em>-</em></a>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney <div class="yui3-bd">
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney <p>This an example of what you can do with the YUI Animation Utility.</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney <p><em>Follow the instructions above to see the animation in action.</em></p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<h2>Using the NodeFX Plugin</h2>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<p>For this example, we will use <code>Node</code>'s <code>fx</code> plugin to animate the element. The plugin adds the anim instance to the <code>Node</code> instance, pre-configuring it to use the Node instance as the <code>Anim</code>'s node. The <code>plug</code> method accepts a class to construct and an optional config to pass to the constructor.</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<p>Setting the <code>from</code> attribute to the expanded height of the element allows us to toggle the effect using the <code>reverse</code> attribute, which we will see below (<code>from</code> uses current value when omitted).</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeneyvar module = Y.one('#demo');
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney// add fx plugin to module body
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeneyvar content = module.one('.yui3-bd').plug(Y.Plugin.NodeFX, {
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney from: { height: 0 },
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney height: function(node) { // dynamic in case of change
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney return node.get('scrollHeight'); // get expanded height (offsetHeight may be zero)
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney from: { height: 0 },
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney duration: 0.5
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<h2>Creating the Control Element</h2>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<p>Because our behavior only works when JS is available, let's go ahead and add our control element using JS as well.</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney// use dynamic control for dynamic behavior
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney '<a title="show/hide content" class="yui3-toggle">' +
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney '<em>toggle</em>' +
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney// append dynamic control to header section
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeneymodule.one('.yui3-hd').appendChild(control);
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<h2>Toggling Animation Behavior</h2>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<p>Before calling <code>run</code> in our <code>click</code> handler, we will use the <code>reverse</code> attribute toggle the direction of the animation depending on whether its opening or closing.</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeneyvar onClick = function(e) {
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney content.fx.set('reverse', !content.fx.get('reverse')); // toggle reverse
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<h2>Running the Animation</h2>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<p>Finally we add an event handler to run the animation.</p>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeneymodule.one('.yui3-toggle').on('click', onClick);
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney<h2>Complete Example Source</h2>
1f08a8488664773a7d96fa3a043a639692d2a5cbMatt Sweeney{{>reverse-source}}