5
<title>Example: Animating Colors</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: Animating Colors</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><link href="../assets/anim/anim.css" rel="stylesheet" type="text/css">
22
<p>This demonstrates how to animate color attributes.</p>
23
<p>Mouse over the link to begin the animation.</p>
27
<a href="#" id="demo">hover me</a>
29
<script type="text/javascript">
30
YUI().use('anim', function(Y) {
31
var node = Y.one('#demo');
33
var anim = new Y.Anim({
36
backgroundColor:node.getStyle('backgroundColor'),
37
color: node.getStyle('color'),
38
borderColor: node.getStyle('borderTopColor')
43
backgroundColor:'#ffa928',
44
borderColor: '#71241a'
50
var hover = function(e) {
52
if (anim.get('running')) {
55
if (e.type === 'mouseout') {
58
anim.set('reverse', reverse);
61
node.on('mouseover', hover);
62
node.on('mouseout', hover);
70
<h2>Setting up the HTML</h2>
71
<p>First we add some HTML to animate.</p>
73
<pre class="code prettyprint"><a href="#" id="demo">hover me</a></pre>
76
<h2>Creating the Anim Instance</h2>
77
<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 and the <code>to</code> attribute containing the final properties and their values.</p>
78
<p>Adding a <code>from</code> attribute allows us to reset the colors <code>onmouseout</code> using the <code>reverse</code> attribute, which we will see below.</p>
80
<pre class="code prettyprint">var node = Y.one('#demo');
82
var anim = new Y.Anim({
85
backgroundColor:node.getStyle('backgroundColor'),
86
color: node.getStyle('color'),
87
borderColor: node.getStyle('borderTopColor')
91
color: '#fff',
92
backgroundColor:'#ffa928',
93
borderColor: '#71241a'
100
<h2>Running the Animation</h2>
101
<p>Next we need a handler to run when the link is moused over, and reverse when moused out.</p>
102
<pre class="code prettyprint">var hover = function(e) {
104
if (anim.get('running')) {
107
if (e.type === 'mouseout') {
110
anim.set('reverse', reverse);
115
<h2>Listening for the Events</h2>
116
<p>Finally we add an event handler to run the animation.</p>
117
<pre class="code prettyprint">node.on('mouseover', hover);
118
node.on('mouseout', hover);</pre>
121
<h2>Complete Example Source</h2>
122
<pre class="code prettyprint"><a href="#" id="demo">hover me</a>
124
<script type="text/javascript">
125
YUI().use('anim', function(Y) {
126
var node = Y.one('#demo');
128
var anim = new Y.Anim({
131
backgroundColor:node.getStyle('backgroundColor'),
132
color: node.getStyle('color'),
133
borderColor: node.getStyle('borderTopColor')
137
color: '#fff',
138
backgroundColor:'#ffa928',
139
borderColor: '#71241a'
145
var hover = function(e) {
147
if (anim.get('running')) {
150
if (e.type === 'mouseout') {
153
anim.set('reverse', reverse);
156
node.on('mouseover', hover);
157
node.on('mouseout', hover);
161
</script></pre>
166
<div id="sidebar" class="yui3-u">
170
<div class="sidebox">
172
<h2 class="no-toc">Examples</h2>
176
<ul class="examples">
179
<li data-description="Creating and using a simple animation.">
180
<a href="basic.html">Basic Animation</a>
185
<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.">
186
<a href="easing.html">Easing Effects</a>
191
<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.">
192
<a href="colors.html">Animating Colors</a>
197
<li data-description="The direction attribute can be used to reverse the animation on alternate iterations.">
198
<a href="alt-iterations.html">Alternating Iterations</a>
203
<li data-description="This example shows you how to animate the xy coordinates of an element.">
204
<a href="anim-xy.html">Animating XY Position</a>
209
<li data-description="This example demonstrates animating an element along a curved path using bezier control points.">
210
<a href="curve.html">Animating Along a Curved Path</a>
215
<li data-description="The reverse attribute allows you to change the run direction of an animation.">
216
<a href="reverse.html">Reversing an Animation</a>
221
<li data-description="This example demonstrates how to use the end event.">
222
<a href="end-event.html">Using the End Event</a>
238
<div class="sidebox">
240
<h2 class="no-toc">Examples That Use This Component</h2>
244
<ul class="examples">
263
<li data-description="Shows how to create a simple plugin to animate the Overlay's movement and visibility.">
264
<a href="../overlay/overlay-anim-plugin.html">Animation Plugin</a>
269
<li data-description="Working with multiple YUI instances.">
270
<a href="../yui/yui-multi.html">Multiple Instances</a>
275
<li data-description="How to make an animated node a Drop target.">
276
<a href="../dd/anim-drop.html">Animated Drop Targets</a>
288
<script src="../assets/vendor/prettify/prettify-min.js"></script>
289
<script>prettyPrint();</script>