|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Reversing an Animation</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Reversing an Animation</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><link href="../assets/anim/anim.css" rel="stylesheet" type="text/css"> |
|
|
29 |
<div class="intro"> |
|
|
30 |
<p>This demonstrates how to use the <code>reverse</code> attribute to change the behavior of the animation.</p> |
|
|
31 |
<p> Click the icon in the header to toggle the element's <code>height</code>.</p> |
|
|
32 |
</div> |
|
|
33 |
|
|
|
34 |
<div class="example"> |
|
|
35 |
<div id="demo" class="yui3-module"> |
|
|
36 |
<div class="yui3-hd"> |
|
|
37 |
<h3>Reversing an Animation</h3> |
|
|
38 |
</div> |
|
|
39 |
<div class="yui3-bd"> |
|
|
40 |
<p>Click the icon in the header to toggle the element's height.</p> |
|
|
41 |
</div> |
|
|
42 |
</div> |
|
|
43 |
<p>This is placeholder text used to demonstrate how the above animation affects subsequent content.</p> |
|
|
44 |
|
|
|
45 |
|
|
|
46 |
<script type="text/javascript"> |
|
|
47 |
YUI().use('anim', function(Y) { |
|
|
48 |
var module = Y.one('#demo'); |
|
|
49 |
|
|
|
50 |
// add fx plugin to module body |
|
|
51 |
var content = module.one('.yui3-bd').plug(Y.Plugin.NodeFX, { |
|
|
52 |
from: { height: 0 }, |
|
|
53 |
to: { |
|
|
54 |
height: function(node) { // dynamic in case of change |
|
|
55 |
return node.get('scrollHeight'); // get expanded height (offsetHeight may be zero) |
|
|
56 |
} |
|
|
57 |
}, |
|
|
58 |
|
|
|
59 |
easing: Y.Easing.easeOut, |
|
|
60 |
duration: 0.5 |
|
|
61 |
}); |
|
|
62 |
|
|
|
63 |
var onClick = function(e) { |
|
|
64 |
e.preventDefault(); |
|
|
65 |
module.toggleClass('yui3-closed'); |
|
|
66 |
content.fx.set('reverse', !content.fx.get('reverse')); // toggle reverse |
|
|
67 |
content.fx.run(); |
|
|
68 |
}; |
|
|
69 |
|
|
|
70 |
// use dynamic control for dynamic behavior |
|
|
71 |
var control = Y.Node.create( |
|
|
72 |
'<a title="collapse/expand element" class="yui3-toggle">' + |
|
|
73 |
'<em>toggle</em>' + |
|
|
74 |
'</a>' |
|
|
75 |
); |
|
|
76 |
|
|
|
77 |
// append dynamic control to header section |
|
|
78 |
module.one('.yui3-hd').appendChild(control); |
|
|
79 |
control.on('click', onClick); |
|
|
80 |
|
|
|
81 |
}); |
|
|
82 |
</script> |
|
|
83 |
|
|
|
84 |
</div> |
|
|
85 |
|
|
|
86 |
<h2>Setting up the HTML</h2> |
|
|
87 |
<p>First we add some HTML to animate.</p> |
|
|
88 |
|
|
|
89 |
<pre class="code prettyprint"><div id="demo" class="yui3-module"> |
|
|
90 |
<div class="yui3-hd"> |
|
|
91 |
<h3>Reversing an Animation</h3> |
|
|
92 |
</div> |
|
|
93 |
<div class="yui3-bd"> |
|
|
94 |
<p>Click the icon in the header to toggle the element's height.</p> |
|
|
95 |
</div> |
|
|
96 |
</div> |
|
|
97 |
<p>This is placeholder text used to demonstrate how the above animation affects subsequent content.</p></pre> |
|
|
98 |
|
|
|
99 |
|
|
|
100 |
<h2>Using the NodeFX Plugin</h2> |
|
|
101 |
<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> |
|
|
102 |
<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> |
|
|
103 |
|
|
|
104 |
<pre class="code prettyprint">var module = Y.one('#demo'); |
|
|
105 |
|
|
|
106 |
// add fx plugin to module body |
|
|
107 |
var content = module.one('.yui3-bd').plug(Y.Plugin.NodeFX, { |
|
|
108 |
from: { height: 0 }, |
|
|
109 |
to: { |
|
|
110 |
height: function(node) { // dynamic in case of change |
|
|
111 |
return node.get('scrollHeight'); // get expanded height (offsetHeight may be zero) |
|
|
112 |
} |
|
|
113 |
}, |
|
|
114 |
|
|
|
115 |
easing: Y.Easing.easeOut, |
|
|
116 |
from: { height: 0 }, |
|
|
117 |
duration: 0.5 |
|
|
118 |
});</pre> |
|
|
119 |
|
|
|
120 |
|
|
|
121 |
<h2>Creating the Control Element</h2> |
|
|
122 |
<p>Because our behavior only works when JS is available, let's go ahead and add our control element using JS as well.</p> |
|
|
123 |
|
|
|
124 |
<pre class="code prettyprint">// use dynamic control for dynamic behavior |
|
|
125 |
var control = Y.Node.create( |
|
|
126 |
'<a title="show/hide content" class="yui3-toggle">' + |
|
|
127 |
'<em>toggle</em>' + |
|
|
128 |
'</a>' |
|
|
129 |
); |
|
|
130 |
|
|
|
131 |
// append dynamic control to header section |
|
|
132 |
module.one('.yui3-hd').appendChild(control);</pre> |
|
|
133 |
|
|
|
134 |
|
|
|
135 |
<h2>Toggling Animation Behavior</h2> |
|
|
136 |
<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> |
|
|
137 |
|
|
|
138 |
<pre class="code prettyprint">var onClick = function(e) { |
|
|
139 |
module.toggleClass('yui-closed'); |
|
|
140 |
content.fx.set('reverse', !content.fx.get('reverse')); // toggle reverse |
|
|
141 |
};</pre> |
|
|
142 |
|
|
|
143 |
|
|
|
144 |
<h2>Running the Animation</h2> |
|
|
145 |
<p>Finally we add an event handler to run the animation.</p> |
|
|
146 |
|
|
|
147 |
<pre class="code prettyprint">module.one('.yui3-toggle').on('click', onClick);</pre> |
|
|
148 |
|
|
|
149 |
|
|
|
150 |
<h2>Complete Example Source</h2> |
|
|
151 |
<pre class="code prettyprint"><div id="demo" class="yui3-module"> |
|
|
152 |
<div class="yui3-hd"> |
|
|
153 |
<h3>Reversing an Animation</h3> |
|
|
154 |
</div> |
|
|
155 |
<div class="yui3-bd"> |
|
|
156 |
<p>Click the icon in the header to toggle the element's height.</p> |
|
|
157 |
</div> |
|
|
158 |
</div> |
|
|
159 |
<p>This is placeholder text used to demonstrate how the above animation affects subsequent content.</p> |
|
|
160 |
|
|
|
161 |
|
|
|
162 |
<script type="text/javascript"> |
|
|
163 |
YUI().use('anim', function(Y) { |
|
|
164 |
var module = Y.one('#demo'); |
|
|
165 |
|
|
|
166 |
// add fx plugin to module body |
|
|
167 |
var content = module.one('.yui3-bd').plug(Y.Plugin.NodeFX, { |
|
|
168 |
from: { height: 0 }, |
|
|
169 |
to: { |
|
|
170 |
height: function(node) { // dynamic in case of change |
|
|
171 |
return node.get('scrollHeight'); // get expanded height (offsetHeight may be zero) |
|
|
172 |
} |
|
|
173 |
}, |
|
|
174 |
|
|
|
175 |
easing: Y.Easing.easeOut, |
|
|
176 |
duration: 0.5 |
|
|
177 |
}); |
|
|
178 |
|
|
|
179 |
var onClick = function(e) { |
|
|
180 |
e.preventDefault(); |
|
|
181 |
module.toggleClass('yui3-closed'); |
|
|
182 |
content.fx.set('reverse', !content.fx.get('reverse')); // toggle reverse |
|
|
183 |
content.fx.run(); |
|
|
184 |
}; |
|
|
185 |
|
|
|
186 |
// use dynamic control for dynamic behavior |
|
|
187 |
var control = Y.Node.create( |
|
|
188 |
'<a title="collapse/expand element" class="yui3-toggle">' + |
|
|
189 |
'<em>toggle</em>' + |
|
|
190 |
'</a>' |
|
|
191 |
); |
|
|
192 |
|
|
|
193 |
// append dynamic control to header section |
|
|
194 |
module.one('.yui3-hd').appendChild(control); |
|
|
195 |
control.on('click', onClick); |
|
|
196 |
|
|
|
197 |
}); |
|
|
198 |
</script></pre> |
|
|
199 |
|
|
|
200 |
</div> |
|
|
201 |
</div> |
|
|
202 |
</div> |
|
|
203 |
|
|
|
204 |
<div class="yui3-u-1-4"> |
|
|
205 |
<div class="sidebar"> |
|
|
206 |
|
|
|
207 |
|
|
|
208 |
|
|
|
209 |
<div class="sidebox"> |
|
|
210 |
<div class="hd"> |
|
|
211 |
<h2 class="no-toc">Examples</h2> |
|
|
212 |
</div> |
|
|
213 |
|
|
|
214 |
<div class="bd"> |
|
|
215 |
<ul class="examples"> |
|
|
216 |
|
|
|
217 |
|
|
|
218 |
<li data-description="Creating and using a simple animation."> |
|
|
219 |
<a href="basic.html">Basic Animation</a> |
|
|
220 |
</li> |
|
|
221 |
|
|
|
222 |
|
|
|
223 |
|
|
|
224 |
<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."> |
|
|
225 |
<a href="easing.html">Easing Effects</a> |
|
|
226 |
</li> |
|
|
227 |
|
|
|
228 |
|
|
|
229 |
|
|
|
230 |
<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."> |
|
|
231 |
<a href="colors.html">Animating Colors</a> |
|
|
232 |
</li> |
|
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
<li data-description="The direction attribute can be used to reverse the animation on alternate iterations."> |
|
|
237 |
<a href="alt-iterations.html">Alternating Iterations</a> |
|
|
238 |
</li> |
|
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
<li data-description="This example shows you how to animate the xy coordinates of an element."> |
|
|
243 |
<a href="anim-xy.html">Animating XY Position</a> |
|
|
244 |
</li> |
|
|
245 |
|
|
|
246 |
|
|
|
247 |
|
|
|
248 |
<li data-description="This example demonstrates animating an element along a curved path using bezier control points."> |
|
|
249 |
<a href="curve.html">Animating Along a Curved Path</a> |
|
|
250 |
</li> |
|
|
251 |
|
|
|
252 |
|
|
|
253 |
|
|
|
254 |
<li data-description="The reverse attribute allows you to change the run direction of an animation."> |
|
|
255 |
<a href="reverse.html">Reversing an Animation</a> |
|
|
256 |
</li> |
|
|
257 |
|
|
|
258 |
|
|
|
259 |
|
|
|
260 |
<li data-description="This example demonstrates how to use the end event."> |
|
|
261 |
<a href="end-event.html">Using the End Event</a> |
|
|
262 |
</li> |
|
|
263 |
|
|
|
264 |
|
|
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
|
|
|
271 |
</ul> |
|
|
272 |
</div> |
|
|
273 |
</div> |
|
|
274 |
|
|
|
275 |
|
|
|
276 |
|
|
|
277 |
<div class="sidebox"> |
|
|
278 |
<div class="hd"> |
|
|
279 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
280 |
</div> |
|
|
281 |
|
|
|
282 |
<div class="bd"> |
|
|
283 |
<ul class="examples"> |
|
|
284 |
|
|
|
285 |
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
|
|
|
289 |
|
|
|
290 |
|
|
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
|
294 |
|
|
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
|
298 |
|
|
|
299 |
|
|
|
300 |
|
|
|
301 |
|
|
|
302 |
<li data-description="Working with multiple YUI instances."> |
|
|
303 |
<a href="../yui/yui-multi.html">Multiple Instances</a> |
|
|
304 |
</li> |
|
|
305 |
|
|
|
306 |
|
|
|
307 |
|
|
|
308 |
<li data-description="Shows how to create a simple plugin to animate the Overlay's movement and visibility."> |
|
|
309 |
<a href="../overlay/overlay-anim-plugin.html">Animation Plugin</a> |
|
|
310 |
</li> |
|
|
311 |
|
|
|
312 |
|
|
|
313 |
|
|
|
314 |
<li data-description="How to make an animated node a Drop target."> |
|
|
315 |
<a href="../dd/anim-drop.html">Animated Drop Targets</a> |
|
|
316 |
</li> |
|
|
317 |
|
|
|
318 |
|
|
|
319 |
</ul> |
|
|
320 |
</div> |
|
|
321 |
</div> |
|
|
322 |
|
|
|
323 |
</div> |
|
|
324 |
</div> |
|
|
325 |
</div> |
|
|
326 |
</div> |
|
|
327 |
|
|
|
328 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
329 |
<script>prettyPrint();</script> |
|
|
330 |
|
|
|
331 |
<script> |
|
|
332 |
YUI.Env.Tests = { |
|
|
333 |
examples: [], |
|
|
334 |
project: '../assets', |
|
|
335 |
assets: '../assets/anim', |
|
|
336 |
name: 'reverse', |
|
|
337 |
title: 'Reversing an Animation', |
|
|
338 |
newWindow: '', |
|
|
339 |
auto: false |
|
|
340 |
}; |
|
|
341 |
YUI.Env.Tests.examples.push('basic'); |
|
|
342 |
YUI.Env.Tests.examples.push('easing'); |
|
|
343 |
YUI.Env.Tests.examples.push('colors'); |
|
|
344 |
YUI.Env.Tests.examples.push('alt-iterations'); |
|
|
345 |
YUI.Env.Tests.examples.push('anim-xy'); |
|
|
346 |
YUI.Env.Tests.examples.push('curve'); |
|
|
347 |
YUI.Env.Tests.examples.push('reverse'); |
|
|
348 |
YUI.Env.Tests.examples.push('end-event'); |
|
|
349 |
YUI.Env.Tests.examples.push('yui-multi'); |
|
|
350 |
YUI.Env.Tests.examples.push('overlay-anim-plugin'); |
|
|
351 |
YUI.Env.Tests.examples.push('anim-drop'); |
|
|
352 |
|
|
|
353 |
</script> |
|
|
354 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
355 |
|
|
|
356 |
|
|
|
357 |
|
|
|
358 |
</body> |
|
|
359 |
</html> |