5
<title>Example: Reversing an Animation</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Example: Reversing an Animation</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><link href="../assets/anim/anim.css" rel="stylesheet" type="text/css">
24
<p>This demonstrates how to use the <code>reverse</code> attribute to change the behavior of the animation.</p>
25
<p> Click the icon in the header to toggle the element's <code>height</code>.</p>
29
<div id="demo" class="yui3-module">
31
<h3>Reversing an Animation</h3>
34
<p>Click the icon in the header to toggle the element's height.</p>
37
<p>This is placeholder text used to demonstrate how the above animation affects subsequent content.</p>
40
<script type="text/javascript">
41
YUI().use('anim', function(Y) {
42
var module = Y.one('#demo');
44
// add fx plugin to module body
45
var content = module.one('.yui3-bd').plug(Y.Plugin.NodeFX, {
48
height: function(node) { // dynamic in case of change
49
return node.get('scrollHeight'); // get expanded height (offsetHeight may be zero)
53
easing: Y.Easing.easeOut,
57
var onClick = function(e) {
58
module.toggleClass('yui3-closed');
59
content.fx.set('reverse', !content.fx.get('reverse')); // toggle reverse
63
// use dynamic control for dynamic behavior
64
var control = Y.Node.create(
65
'<a title="collapse/expand element" class="yui3-toggle">' +
70
// append dynamic control to header section
71
module.one('.yui3-hd').appendChild(control);
72
control.on('click', onClick);
79
<h2>Setting up the HTML</h2>
80
<p>First we add some HTML to animate.</p>
82
<pre class="code prettyprint"><div id="demo" class="yui3-module">
83
<div class="yui3-hd">
84
<h3>Reversing an Animation</h3>
86
<div class="yui3-bd">
87
<p>Click the icon in the header to toggle the element's height.</p>
90
<p>This is placeholder text used to demonstrate how the above animation affects subsequent content.</p></pre>
93
<h2>Using the NodeFX Plugin</h2>
94
<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>
95
<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>
97
<pre class="code prettyprint">var module = Y.one('#demo');
99
// add fx plugin to module body
100
var content = module.one('.yui3-bd').plug(Y.Plugin.NodeFX, {
103
height: function(node) { // dynamic in case of change
104
return node.get('scrollHeight'); // get expanded height (offsetHeight may be zero)
108
easing: Y.Easing.easeOut,
114
<h2>Creating the Control Element</h2>
115
<p>Because our behavior only works when JS is available, let's go ahead and add our control element using JS as well.</p>
117
<pre class="code prettyprint">// use dynamic control for dynamic behavior
118
var control = Y.Node.create(
119
'<a title="show/hide content" class="yui3-toggle">' +
120
'<em>toggle</em>' +
121
'</a>'
124
// append dynamic control to header section
125
module.one('.yui3-hd').appendChild(control);</pre>
128
<h2>Toggling Animation Behavior</h2>
129
<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>
131
<pre class="code prettyprint">var onClick = function(e) {
132
module.toggleClass('yui-closed');
133
content.fx.set('reverse', !content.fx.get('reverse')); // toggle reverse
137
<h2>Running the Animation</h2>
138
<p>Finally we add an event handler to run the animation.</p>
140
<pre class="code prettyprint">module.one('.yui3-toggle').on('click', onClick);</pre>
143
<h2>Complete Example Source</h2>
144
<pre class="code prettyprint"><div id="demo" class="yui3-module">
145
<div class="yui3-hd">
146
<h3>Reversing an Animation</h3>
148
<div class="yui3-bd">
149
<p>Click the icon in the header to toggle the element's height.</p>
152
<p>This is placeholder text used to demonstrate how the above animation affects subsequent content.</p>
155
<script type="text/javascript">
156
YUI().use('anim', function(Y) {
157
var module = Y.one('#demo');
159
// add fx plugin to module body
160
var content = module.one('.yui3-bd').plug(Y.Plugin.NodeFX, {
163
height: function(node) { // dynamic in case of change
164
return node.get('scrollHeight'); // get expanded height (offsetHeight may be zero)
168
easing: Y.Easing.easeOut,
172
var onClick = function(e) {
173
module.toggleClass('yui3-closed');
174
content.fx.set('reverse', !content.fx.get('reverse')); // toggle reverse
178
// use dynamic control for dynamic behavior
179
var control = Y.Node.create(
180
'<a title="collapse/expand element" class="yui3-toggle">' +
181
'<em>toggle</em>' +
182
'</a>'
185
// append dynamic control to header section
186
module.one('.yui3-hd').appendChild(control);
187
control.on('click', onClick);
190
</script></pre>
196
<div class="yui3-u-1-4">
197
<div class="sidebar">
201
<div class="sidebox">
203
<h2 class="no-toc">Examples</h2>
207
<ul class="examples">
210
<li data-description="Creating and using a simple animation.">
211
<a href="basic.html">Basic Animation</a>
216
<li data-description="The Animation Utility allows you to implement 'easing effects' — for example, when an animation gradually slows down as it nears completion, that's an easing effect known as 'ease-in'. This example shows you how to use easing effects with your animations.">
217
<a href="easing.html">Easing Effects</a>
222
<li data-description="Color animations can be effective indicators of state during the lifespan of a dynamic page. This example shows you how to animate color attributes of an element.">
223
<a href="colors.html">Animating Colors</a>
228
<li data-description="The direction attribute can be used to reverse the animation on alternate iterations.">
229
<a href="alt-iterations.html">Alternating Iterations</a>
234
<li data-description="This example shows you how to animate the xy coordinates of an element.">
235
<a href="anim-xy.html">Animating XY Position</a>
240
<li data-description="This example demonstrates animating an element along a curved path using bezier control points.">
241
<a href="curve.html">Animating Along a Curved Path</a>
246
<li data-description="The reverse attribute allows you to change the run direction of an animation.">
247
<a href="reverse.html">Reversing an Animation</a>
252
<li data-description="This example demonstrates how to use the end event.">
253
<a href="end-event.html">Using the End Event</a>
269
<div class="sidebox">
271
<h2 class="no-toc">Examples That Use This Component</h2>
275
<ul class="examples">
294
<li data-description="Shows how to create a simple plugin to animate the Overlay's movement and visibility.">
295
<a href="../overlay/overlay-anim-plugin.html">Animation Plugin</a>
300
<li data-description="Working with multiple YUI instances.">
301
<a href="../yui/yui-multi.html">Multiple Instances</a>
306
<li data-description="How to make an animated node a Drop target.">
307
<a href="../dd/anim-drop.html">Animated Drop Targets</a>
320
<script src="../assets/vendor/prettify/prettify-min.js"></script>
321
<script>prettyPrint();</script>