|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Using NodeList - Ducks Game</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: Using NodeList - Ducks Game</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><link href="../assets/node/node.css" rel="stylesheet" type="text/css"> |
|
|
29 |
<div class="intro"> |
|
|
30 |
<p>This example demonstrates how to use multiple NodeList features to build a simple game.</p> |
|
|
31 |
</div> |
|
|
32 |
|
|
|
33 |
<div class="example"> |
|
|
34 |
<style> |
|
|
35 |
.gallery{ |
|
|
36 |
position: relative; |
|
|
37 |
background: url(../assets/node/images/background.png); |
|
|
38 |
width: 638px; |
|
|
39 |
height: 180px; |
|
|
40 |
overflow: hidden; |
|
|
41 |
-moz-border-radius: 8px; |
|
|
42 |
-webkit-border-radius: 8px; |
|
|
43 |
border-radius: 8px; |
|
|
44 |
-moz-box-shadow: 0 0 45px #000 inset, 3px 0 6px rgba(0,0,0,0.8); |
|
|
45 |
-webkit-box-shadow: 0 0 45px #000 inset, 3px 0 6px rgba(0,0,0,0.8); |
|
|
46 |
border: 4px solid #637073; |
|
|
47 |
cursor: crosshair; |
|
|
48 |
margin: 0.5em 0 0.8em; |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
.water{ |
|
|
52 |
position: absolute; |
|
|
53 |
top: 114px; |
|
|
54 |
left: 0; |
|
|
55 |
width: 677px; |
|
|
56 |
height: 50px; |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
.water img{ |
|
|
60 |
position: absolute; |
|
|
61 |
top: 0; |
|
|
62 |
left: 0; |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
.water .shadow { |
|
|
66 |
left: 8px; |
|
|
67 |
opacity: 0.5; |
|
|
68 |
filter: alpha(opacity=30); |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
.duck-row { |
|
|
72 |
position: absolute; |
|
|
73 |
left: 10px; |
|
|
74 |
top: 57px; |
|
|
75 |
width: 1340px; |
|
|
76 |
padding: 0; |
|
|
77 |
margin: 0; |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
.duck-row li{ |
|
|
81 |
position: relative; |
|
|
82 |
display: inline-block; |
|
|
83 |
zoom: 1; *display: inline; /* IE < 8: fake inline-block */ |
|
|
84 |
width: 133px; |
|
|
85 |
height: 70px; |
|
|
86 |
margin: 0 -4px 0 0; |
|
|
87 |
vertical-align: bottom; |
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
.duck-row li img{ |
|
|
91 |
position: absolute; |
|
|
92 |
bottom: 7px; |
|
|
93 |
left: 0; |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
/* the voice bubble of the ducks */ |
|
|
97 |
.duck-row .squawk { |
|
|
98 |
position: absolute; |
|
|
99 |
top: -400px; |
|
|
100 |
left: 50px; |
|
|
101 |
-moz-border-radius: 4px; |
|
|
102 |
-webkit-border-radius: 4px; |
|
|
103 |
border-radius: 4px; |
|
|
104 |
background-color: #ffe; |
|
|
105 |
line-height: 1.2em; |
|
|
106 |
border: solid 1px #cc8; |
|
|
107 |
-moz-box-shadow: 2px 2px 2px rgba(0,0,0,0.2); |
|
|
108 |
-webkit-box-shadow: 2px 2px 2px rgba(0,0,0,0.2); |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
.duck-row .squawk .text{ |
|
|
112 |
padding: 0.2em 0.4em 0.2em 0em; |
|
|
113 |
text-indent: 0.2em; |
|
|
114 |
overflow: hidden; |
|
|
115 |
} |
|
|
116 |
|
|
|
117 |
.small-squawk-bubble{ |
|
|
118 |
position: absolute; |
|
|
119 |
bottom: -4px; |
|
|
120 |
left: -8px; |
|
|
121 |
width: 4px; |
|
|
122 |
height: 4px; |
|
|
123 |
border: solid 1px #cc8; |
|
|
124 |
-moz-border-radius: 3px; |
|
|
125 |
-webkit-border-radius: 3px; |
|
|
126 |
border-radius: 3px; |
|
|
127 |
background-color: #ffe; |
|
|
128 |
font-size: 2px; |
|
|
129 |
} |
|
|
130 |
|
|
|
131 |
#button-reset{ |
|
|
132 |
margin-right: 2em; |
|
|
133 |
} |
|
|
134 |
|
|
|
135 |
.ducks-remain{ |
|
|
136 |
font-size: 150%; |
|
|
137 |
} |
|
|
138 |
|
|
|
139 |
#show-attitude{ |
|
|
140 |
margin-left: 8em; |
|
|
141 |
} |
|
|
142 |
</style> |
|
|
143 |
|
|
|
144 |
<div class='gallery'> |
|
|
145 |
<ul class="duck-row"> |
|
|
146 |
<li></li> <!-- each of these will be a duck --> |
|
|
147 |
<li></li> |
|
|
148 |
<li></li> |
|
|
149 |
<li></li> |
|
|
150 |
<li></li> |
|
|
151 |
<li></li> |
|
|
152 |
<li></li> |
|
|
153 |
<li></li> |
|
|
154 |
<li></li> |
|
|
155 |
<li></li> |
|
|
156 |
</ul> |
|
|
157 |
<div class="water"> |
|
|
158 |
<img class="shadow" src="../assets/node/images/water-shadow.png"/> |
|
|
159 |
<img src="../assets/node/images/water.png"/> |
|
|
160 |
</div> |
|
|
161 |
</div> |
|
|
162 |
<button id="button-reset" class="yui3-button">Reset</button> |
|
|
163 |
Ducks remaining: <span class="ducks-remain">10</span> |
|
|
164 |
<input type="checkbox" id="show-attitude"/><label for="show-attitude">Show attitude</label> |
|
|
165 |
|
|
|
166 |
<script> |
|
|
167 |
YUI().use('transition', 'button', function (Y) { |
|
|
168 |
|
|
|
169 |
var ducks = Y.all('.duck-row li'), |
|
|
170 |
ducksRemaining = 10, // value for UI display |
|
|
171 |
squawkTextIndex = 0, // index in the squawkTextArr to use next |
|
|
172 |
squawkTextArr = [ // duck comments |
|
|
173 |
'#@&~*Q!', |
|
|
174 |
'Hey!?', |
|
|
175 |
'911 on U!', |
|
|
176 |
"U fly's down", |
|
|
177 |
'duck pile!', |
|
|
178 |
'Ricochets kill!', |
|
|
179 |
'how sporting.', |
|
|
180 |
"shoe's untied" |
|
|
181 |
]; |
|
|
182 |
|
|
|
183 |
// append the same content for each duck <li> |
|
|
184 |
ducks.append('<img src="../assets/node/images/duck.png"/><div class="squawk"><div class="text">#@&~*Q!</div><div class="small-squawk-bubble"></div></div>'); |
|
|
185 |
|
|
|
186 |
// give them all the set-up state class |
|
|
187 |
ducks.addClass('set-up'); |
|
|
188 |
|
|
|
189 |
// this makes the ducks move from right to left. |
|
|
190 |
// When the duck on the far left disappears from view, |
|
|
191 |
// it's added to the far right end of the row. |
|
|
192 |
var makeDucksSwim = function () { |
|
|
193 |
var frontDuck; |
|
|
194 |
|
|
|
195 |
// move the duck row to the left one duck space over 2 seconds |
|
|
196 |
Y.one('.duck-row').transition({ |
|
|
197 |
easing: 'linear', |
|
|
198 |
left: '-119px', |
|
|
199 |
duration: 2 |
|
|
200 |
}, function () { // when the row finishes its right to left transition... |
|
|
201 |
// remove the first duck on the left |
|
|
202 |
// which has trasitioned out of view |
|
|
203 |
frontDuck = Y.one('.duck-row li').remove(); |
|
|
204 |
|
|
|
205 |
// append the removed first duck onto the right end |
|
|
206 |
Y.one('.duck-row').appendChild(frontDuck); |
|
|
207 |
|
|
|
208 |
// set the position for the next makeDucksSwim() |
|
|
209 |
Y.one('.duck-row').setStyle('left', '10px'); |
|
|
210 |
|
|
|
211 |
// if there are ducks remaining, make them swim again |
|
|
212 |
if (ducksRemaining > 0) { |
|
|
213 |
makeDucksSwim(); |
|
|
214 |
} |
|
|
215 |
}); |
|
|
216 |
} |
|
|
217 |
makeDucksSwim(); // this initializes the ducks swimming |
|
|
218 |
|
|
|
219 |
// handles a click on a duck |
|
|
220 |
var duckClick = function(e) { |
|
|
221 |
var squawks; |
|
|
222 |
|
|
|
223 |
// remove the squawk belonging to the duck that was clicked |
|
|
224 |
e.currentTarget.one('.squawk').setStyles({'top': '-400px', 'opacity': '1'}); |
|
|
225 |
|
|
|
226 |
// makes the ducks appear to lay back when clicked |
|
|
227 |
e.target.transition({ |
|
|
228 |
duration: 0.2, |
|
|
229 |
height: '3px', |
|
|
230 |
width: '133px' |
|
|
231 |
}); |
|
|
232 |
|
|
|
233 |
// the clicked duck will no longer have the 'set-up' class/state |
|
|
234 |
e.currentTarget.removeClass('set-up'); |
|
|
235 |
makeDucksSquawk(); |
|
|
236 |
updateDucksRemaining(); // update the number of ducks still set up |
|
|
237 |
}; |
|
|
238 |
|
|
|
239 |
// this makes the duck's squawks show and hide and get various text |
|
|
240 |
var makeDucksSquawk = function(){ |
|
|
241 |
squawks = Y.all('.duck-row .set-up .squawk'); // a NodeList of the squawks of set-up ducks |
|
|
242 |
if (Y.one('#show-attitude')._node.checked) { // only have ducks squawk if the checkbox is checked |
|
|
243 |
// fill voice bubbles with next text string |
|
|
244 |
Y.all('.duck-row .set-up .squawk .text').setHTML(squawkTextArr[squawkTextIndex]); |
|
|
245 |
|
|
|
246 |
// increment the index to get the next squawk text |
|
|
247 |
squawkTextIndex = (squawkTextIndex += 1) % (squawkTextArr.length); |
|
|
248 |
squawks.transition({ |
|
|
249 |
top: { |
|
|
250 |
delay: 0.5, |
|
|
251 |
value: '0px', // drop squawks into position from hidden |
|
|
252 |
duration: 0 // instant position change |
|
|
253 |
}, |
|
|
254 |
opacity: { // fade out |
|
|
255 |
delay: 3.0, |
|
|
256 |
duration: 0.3, |
|
|
257 |
value: 0 |
|
|
258 |
} |
|
|
259 |
}, function(e){ |
|
|
260 |
// after squawks are faded out, |
|
|
261 |
// move them to hidden position and set opacity to 1 again |
|
|
262 |
squawks.setStyles({'top': '-400px', 'opacity': '1'}); |
|
|
263 |
}); |
|
|
264 |
} |
|
|
265 |
} |
|
|
266 |
|
|
|
267 |
// This resets all ducks, "ducks remaining" counters, and row position |
|
|
268 |
// make the duck images full height |
|
|
269 |
// start them swimming |
|
|
270 |
var reset = function() { |
|
|
271 |
Y.all('.duck-row li img').setStyle('height', '55px'); |
|
|
272 |
Y.all('.duck-row li').addClass('set-up'); |
|
|
273 |
updateDucksRemaining(); |
|
|
274 |
makeDucksSwim(); |
|
|
275 |
} |
|
|
276 |
|
|
|
277 |
// counts the ducks remaining, and updates the UI counter display |
|
|
278 |
var updateDucksRemaining = function() { |
|
|
279 |
ducksRemaining = Y.all('.gallery li.set-up').size(); |
|
|
280 |
Y.one('.ducks-remain').setHTML(ducksRemaining); |
|
|
281 |
} |
|
|
282 |
|
|
|
283 |
// listeners |
|
|
284 |
Y.one('.duck-row').delegate('click', duckClick, 'li'); |
|
|
285 |
Y.one('#button-reset').on('click', reset); |
|
|
286 |
}); |
|
|
287 |
</script> |
|
|
288 |
|
|
|
289 |
|
|
|
290 |
</div> |
|
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
|
294 |
<h2>The HTML</h2> |
|
|
295 |
<pre class="code prettyprint"><div class='gallery'> |
|
|
296 |
<ul class="duck-row"> |
|
|
297 |
<li></li> <!-- each of these will be a duck --> |
|
|
298 |
<li></li> |
|
|
299 |
<li></li> |
|
|
300 |
<li></li> |
|
|
301 |
<li></li> |
|
|
302 |
<li></li> |
|
|
303 |
<li></li> |
|
|
304 |
<li></li> |
|
|
305 |
<li></li> |
|
|
306 |
<li></li> |
|
|
307 |
</ul> |
|
|
308 |
<div class="water"> |
|
|
309 |
<img class="shadow" src="../assets/node/images/water-shadow.png"/> |
|
|
310 |
<img src="../assets/node/images/water.png"/> |
|
|
311 |
</div> |
|
|
312 |
</div> |
|
|
313 |
<button id="button-reset" class="yui3-button">Reset</button> |
|
|
314 |
Ducks remaining: <span class="ducks-remain">10</span> |
|
|
315 |
<input type="checkbox" id="show-attitude"/><label for="show-attitude">Show attitude</label></pre> |
|
|
316 |
|
|
|
317 |
|
|
|
318 |
<h2>YUI Instance</h2> |
|
|
319 |
<p> |
|
|
320 |
The <code>use</code> statement doesn't include <code>node</code> because it's loaded as a requirement of transition. |
|
|
321 |
</p> |
|
|
322 |
<pre class="code prettyprint">YUI().use('transition', 'button', function(Y){ |
|
|
323 |
// code goes here |
|
|
324 |
});</pre> |
|
|
325 |
|
|
|
326 |
|
|
|
327 |
<h2>Setting Vars</h2> |
|
|
328 |
<p> |
|
|
329 |
The variable <code>ducks</code> is used for easily manipulating all the ducks at once. |
|
|
330 |
We'll display various duck comments from the array <code>squawkTextArr</code> on a rotating basis. |
|
|
331 |
</p> |
|
|
332 |
<pre class="code prettyprint">var ducks = Y.all('.duck-row li'), |
|
|
333 |
ducksRemaining = 10, // value for UI display |
|
|
334 |
squawkTextIndex = 0, // index in the squawkTextArr to use next |
|
|
335 |
squawkTextArr = [ // duck comments |
|
|
336 |
'#@&~*Q!', |
|
|
337 |
'Hey!?', |
|
|
338 |
'911 on U!', |
|
|
339 |
"U&nbsp;fly's&nbsp;down", |
|
|
340 |
'duck&nbsp;pile!', |
|
|
341 |
'Ricochets&nbsp;kill!', |
|
|
342 |
'how&nbsp;sporting.', |
|
|
343 |
"shoe's&nbsp;untied" |
|
|
344 |
];</pre> |
|
|
345 |
|
|
|
346 |
|
|
|
347 |
<h2>Initializing the Ducks</h2> |
|
|
348 |
<p> |
|
|
349 |
Repetitive markup is added with the <code>.append()</code> method to all the ducks in the <code>NodeList</code>. |
|
|
350 |
This keeps the original markup simple and clear. |
|
|
351 |
</p> |
|
|
352 |
<pre class="code prettyprint">// append the same content for each duck <li> |
|
|
353 |
ducks.append('<img src="../assets/node/images/duck.png"/><div class="squawk"><div class="text">#@&~*Q!</div><div class="small-squawk-bubble"></div></div>');</pre> |
|
|
354 |
|
|
|
355 |
<p> |
|
|
356 |
All the ducks in the <code>NodeList</code> are given the <code>set-up</code> class with the <code>.addClass()</code> method. |
|
|
357 |
This class is found on any duck that has the state of being set up, as opposed to being knocked down. |
|
|
358 |
</p> |
|
|
359 |
<pre class="code prettyprint">// give them all the set-up state class |
|
|
360 |
ducks.addClass('set-up');</pre> |
|
|
361 |
|
|
|
362 |
<p> |
|
|
363 |
This state could be a Boolean property, but it's handy as a class, |
|
|
364 |
because a <code>NodeList</code> can be made containing the squawks of all "set up" ducks in this way, |
|
|
365 |
<code>squawks = Y.all('.duck-row .set-up .squawk');</code> |
|
|
366 |
as we'll see in the <code>makeDucksSquawk</code> function. |
|
|
367 |
</p> |
|
|
368 |
|
|
|
369 |
<h2>Making the Ducks Swim</h2> |
|
|
370 |
<p> |
|
|
371 |
This uses <code>transition</code> to make the ducks swim right to left |
|
|
372 |
</p> |
|
|
373 |
<pre class="code prettyprint">// this makes the ducks move from right to left. |
|
|
374 |
// When the duck on the far left disappears from view, |
|
|
375 |
// it's added to the far right end of the row. |
|
|
376 |
var makeDucksSwim = function () { |
|
|
377 |
var frontDuck; |
|
|
378 |
|
|
|
379 |
// move the duck row to the left one duck space over 2 seconds |
|
|
380 |
Y.one('.duck-row').transition({ |
|
|
381 |
easing: 'linear', |
|
|
382 |
left: '-119px', |
|
|
383 |
duration: 2 |
|
|
384 |
}, function () { // when the row finishes its right to left transition... |
|
|
385 |
// remove the first duck on the left |
|
|
386 |
// which has trasitioned out of view |
|
|
387 |
frontDuck = Y.one('.duck-row li').remove(); |
|
|
388 |
|
|
|
389 |
// append the removed first duck onto the right end |
|
|
390 |
Y.one('.duck-row').appendChild(frontDuck); |
|
|
391 |
|
|
|
392 |
// set the position for the next makeDucksSwim() |
|
|
393 |
Y.one('.duck-row').setStyle('left', '10px'); |
|
|
394 |
|
|
|
395 |
// if there are ducks remaining, make them swim again |
|
|
396 |
if (ducksRemaining > 0) { |
|
|
397 |
makeDucksSwim(); |
|
|
398 |
} |
|
|
399 |
}); |
|
|
400 |
} |
|
|
401 |
makeDucksSwim(); // this initializes the ducks swimming</pre> |
|
|
402 |
|
|
|
403 |
|
|
|
404 |
<h2>Click Event Handler</h2> |
|
|
405 |
<p> |
|
|
406 |
</p> |
|
|
407 |
<pre class="code prettyprint">// handles a click on a duck |
|
|
408 |
var duckClick = function(e) { |
|
|
409 |
var squawks; |
|
|
410 |
|
|
|
411 |
// remove the squawk belonging to the duck that was clicked |
|
|
412 |
e.currentTarget.one('.squawk').setStyles({'top': '-400px', 'opacity': '1'}); |
|
|
413 |
|
|
|
414 |
// makes the ducks appear to lay back when clicked |
|
|
415 |
e.target.transition({ |
|
|
416 |
duration: 0.2, |
|
|
417 |
height: '3px', |
|
|
418 |
width: '133px' |
|
|
419 |
}); |
|
|
420 |
|
|
|
421 |
// the clicked duck will no longer have the 'set-up' class/state |
|
|
422 |
e.currentTarget.removeClass('set-up'); |
|
|
423 |
makeDucksSquawk(); // makes the ducks squawk |
|
|
424 |
updateDucksRemaining(); // update the number of ducks still set up |
|
|
425 |
};</pre> |
|
|
426 |
|
|
|
427 |
|
|
|
428 |
<h2>Squawking Ducks</h2> |
|
|
429 |
<p> |
|
|
430 |
</p> |
|
|
431 |
<pre class="code prettyprint">// this makes the duck's squawks show and hide and get various text |
|
|
432 |
var makeDucksSquawk = function(){ |
|
|
433 |
squawks = Y.all('.duck-row .set-up .squawk'); // a NodeList of the squawks of set-up ducks |
|
|
434 |
if (Y.one('#show-attitude')._node.checked) { // only have ducks squawk if the checkbox is checked |
|
|
435 |
// fill voice bubbles with next text string |
|
|
436 |
Y.all('.duck-row .set-up .squawk .text').setHTML(squawkTextArr[squawkTextIndex]); |
|
|
437 |
|
|
|
438 |
// increment the index to get the next squawk text |
|
|
439 |
squawkTextIndex = (squawkTextIndex += 1) % (squawkTextArr.length); |
|
|
440 |
squawks.transition({ |
|
|
441 |
top: { |
|
|
442 |
delay: 0.5, |
|
|
443 |
value: '0px', // drop squawks into position from hidden |
|
|
444 |
duration: 0 // instant position change |
|
|
445 |
}, |
|
|
446 |
opacity: { // fade out |
|
|
447 |
delay: 3.0, |
|
|
448 |
duration: 0.3, |
|
|
449 |
value: 0 |
|
|
450 |
} |
|
|
451 |
}, function(e){ |
|
|
452 |
// after squawks are faded out, |
|
|
453 |
// move them to hidden position and set opacity to 1 again |
|
|
454 |
squawks.setStyles({'top': '-400px', 'opacity': '1'}); |
|
|
455 |
}); |
|
|
456 |
} |
|
|
457 |
}</pre> |
|
|
458 |
|
|
|
459 |
|
|
|
460 |
<h2>Reset and Ducks Remaining</h2> |
|
|
461 |
<pre class="code prettyprint">// This resets all ducks, "ducks remaining" counters, and row position |
|
|
462 |
// make the duck images full height |
|
|
463 |
// start them swimming |
|
|
464 |
var reset = function() { |
|
|
465 |
Y.all('.duck-row li img').setStyle('height', '55px'); |
|
|
466 |
Y.all('.duck-row li').addClass('set-up'); |
|
|
467 |
updateDucksRemaining(); |
|
|
468 |
makeDucksSwim(); |
|
|
469 |
} |
|
|
470 |
|
|
|
471 |
// counts the ducks remaining, and updates the UI counter display |
|
|
472 |
var updateDucksRemaining = function() { |
|
|
473 |
ducksRemaining = Y.all('.gallery li.set-up').size(); |
|
|
474 |
Y.one('.ducks-remain').setHTML(ducksRemaining); |
|
|
475 |
}</pre> |
|
|
476 |
|
|
|
477 |
<h2>Prefer <code>node.delegate()</code> over <code>nodelist.on()</code></h2> |
|
|
478 |
|
|
|
479 |
<p>Sometimes you need to create individual subscriptions for each Node in a |
|
|
480 |
NodeList, but usually it's preferable to use |
|
|
481 |
<a href="node-evt-delegation.html">event delegation</a> as shown in this example.</p> |
|
|
482 |
|
|
|
483 |
<h2>Listeners</h2> |
|
|
484 |
<p> |
|
|
485 |
|
|
|
486 |
</p> |
|
|
487 |
<pre class="code prettyprint">// listeners |
|
|
488 |
Y.one('.duck-row').delegate('click', duckClick, 'li'); |
|
|
489 |
Y.one('#button-reset').on('click', reset);</pre> |
|
|
490 |
|
|
|
491 |
|
|
|
492 |
<h2>Complete Ducks Example Source</h2> |
|
|
493 |
<pre class="code prettyprint"><style> |
|
|
494 |
.gallery{ |
|
|
495 |
position: relative; |
|
|
496 |
background: url(../assets/node/images/background.png); |
|
|
497 |
width: 638px; |
|
|
498 |
height: 180px; |
|
|
499 |
overflow: hidden; |
|
|
500 |
-moz-border-radius: 8px; |
|
|
501 |
-webkit-border-radius: 8px; |
|
|
502 |
border-radius: 8px; |
|
|
503 |
-moz-box-shadow: 0 0 45px #000 inset, 3px 0 6px rgba(0,0,0,0.8); |
|
|
504 |
-webkit-box-shadow: 0 0 45px #000 inset, 3px 0 6px rgba(0,0,0,0.8); |
|
|
505 |
border: 4px solid #637073; |
|
|
506 |
cursor: crosshair; |
|
|
507 |
margin: 0.5em 0 0.8em; |
|
|
508 |
} |
|
|
509 |
|
|
|
510 |
.water{ |
|
|
511 |
position: absolute; |
|
|
512 |
top: 114px; |
|
|
513 |
left: 0; |
|
|
514 |
width: 677px; |
|
|
515 |
height: 50px; |
|
|
516 |
} |
|
|
517 |
|
|
|
518 |
.water img{ |
|
|
519 |
position: absolute; |
|
|
520 |
top: 0; |
|
|
521 |
left: 0; |
|
|
522 |
} |
|
|
523 |
|
|
|
524 |
.water .shadow { |
|
|
525 |
left: 8px; |
|
|
526 |
opacity: 0.5; |
|
|
527 |
filter: alpha(opacity=30); |
|
|
528 |
} |
|
|
529 |
|
|
|
530 |
.duck-row { |
|
|
531 |
position: absolute; |
|
|
532 |
left: 10px; |
|
|
533 |
top: 57px; |
|
|
534 |
width: 1340px; |
|
|
535 |
padding: 0; |
|
|
536 |
margin: 0; |
|
|
537 |
} |
|
|
538 |
|
|
|
539 |
.duck-row li{ |
|
|
540 |
position: relative; |
|
|
541 |
display: inline-block; |
|
|
542 |
zoom: 1; *display: inline; /* IE < 8: fake inline-block */ |
|
|
543 |
width: 133px; |
|
|
544 |
height: 70px; |
|
|
545 |
margin: 0 -4px 0 0; |
|
|
546 |
vertical-align: bottom; |
|
|
547 |
} |
|
|
548 |
|
|
|
549 |
.duck-row li img{ |
|
|
550 |
position: absolute; |
|
|
551 |
bottom: 7px; |
|
|
552 |
left: 0; |
|
|
553 |
} |
|
|
554 |
|
|
|
555 |
/* the voice bubble of the ducks */ |
|
|
556 |
.duck-row .squawk { |
|
|
557 |
position: absolute; |
|
|
558 |
top: -400px; |
|
|
559 |
left: 50px; |
|
|
560 |
-moz-border-radius: 4px; |
|
|
561 |
-webkit-border-radius: 4px; |
|
|
562 |
border-radius: 4px; |
|
|
563 |
background-color: #ffe; |
|
|
564 |
line-height: 1.2em; |
|
|
565 |
border: solid 1px #cc8; |
|
|
566 |
-moz-box-shadow: 2px 2px 2px rgba(0,0,0,0.2); |
|
|
567 |
-webkit-box-shadow: 2px 2px 2px rgba(0,0,0,0.2); |
|
|
568 |
} |
|
|
569 |
|
|
|
570 |
.duck-row .squawk .text{ |
|
|
571 |
padding: 0.2em 0.4em 0.2em 0em; |
|
|
572 |
text-indent: 0.2em; |
|
|
573 |
overflow: hidden; |
|
|
574 |
} |
|
|
575 |
|
|
|
576 |
.small-squawk-bubble{ |
|
|
577 |
position: absolute; |
|
|
578 |
bottom: -4px; |
|
|
579 |
left: -8px; |
|
|
580 |
width: 4px; |
|
|
581 |
height: 4px; |
|
|
582 |
border: solid 1px #cc8; |
|
|
583 |
-moz-border-radius: 3px; |
|
|
584 |
-webkit-border-radius: 3px; |
|
|
585 |
border-radius: 3px; |
|
|
586 |
background-color: #ffe; |
|
|
587 |
font-size: 2px; |
|
|
588 |
} |
|
|
589 |
|
|
|
590 |
#button-reset{ |
|
|
591 |
margin-right: 2em; |
|
|
592 |
} |
|
|
593 |
|
|
|
594 |
.ducks-remain{ |
|
|
595 |
font-size: 150%; |
|
|
596 |
} |
|
|
597 |
|
|
|
598 |
#show-attitude{ |
|
|
599 |
margin-left: 8em; |
|
|
600 |
} |
|
|
601 |
</style> |
|
|
602 |
|
|
|
603 |
<div class='gallery'> |
|
|
604 |
<ul class="duck-row"> |
|
|
605 |
<li></li> <!-- each of these will be a duck --> |
|
|
606 |
<li></li> |
|
|
607 |
<li></li> |
|
|
608 |
<li></li> |
|
|
609 |
<li></li> |
|
|
610 |
<li></li> |
|
|
611 |
<li></li> |
|
|
612 |
<li></li> |
|
|
613 |
<li></li> |
|
|
614 |
<li></li> |
|
|
615 |
</ul> |
|
|
616 |
<div class="water"> |
|
|
617 |
<img class="shadow" src="../assets/node/images/water-shadow.png"/> |
|
|
618 |
<img src="../assets/node/images/water.png"/> |
|
|
619 |
</div> |
|
|
620 |
</div> |
|
|
621 |
<button id="button-reset" class="yui3-button">Reset</button> |
|
|
622 |
Ducks remaining: <span class="ducks-remain">10</span> |
|
|
623 |
<input type="checkbox" id="show-attitude"/><label for="show-attitude">Show attitude</label> |
|
|
624 |
|
|
|
625 |
<script> |
|
|
626 |
YUI().use('transition', 'button', function (Y) { |
|
|
627 |
|
|
|
628 |
var ducks = Y.all('.duck-row li'), |
|
|
629 |
ducksRemaining = 10, // value for UI display |
|
|
630 |
squawkTextIndex = 0, // index in the squawkTextArr to use next |
|
|
631 |
squawkTextArr = [ // duck comments |
|
|
632 |
'#@&~*Q!', |
|
|
633 |
'Hey!?', |
|
|
634 |
'911 on U!', |
|
|
635 |
"U&nbsp;fly's&nbsp;down", |
|
|
636 |
'duck&nbsp;pile!', |
|
|
637 |
'Ricochets&nbsp;kill!', |
|
|
638 |
'how&nbsp;sporting.', |
|
|
639 |
"shoe's&nbsp;untied" |
|
|
640 |
]; |
|
|
641 |
|
|
|
642 |
// append the same content for each duck <li> |
|
|
643 |
ducks.append('<img src="../assets/node/images/duck.png"/><div class="squawk"><div class="text">#@&~*Q!</div><div class="small-squawk-bubble"></div></div>'); |
|
|
644 |
|
|
|
645 |
// give them all the set-up state class |
|
|
646 |
ducks.addClass('set-up'); |
|
|
647 |
|
|
|
648 |
// this makes the ducks move from right to left. |
|
|
649 |
// When the duck on the far left disappears from view, |
|
|
650 |
// it's added to the far right end of the row. |
|
|
651 |
var makeDucksSwim = function () { |
|
|
652 |
var frontDuck; |
|
|
653 |
|
|
|
654 |
// move the duck row to the left one duck space over 2 seconds |
|
|
655 |
Y.one('.duck-row').transition({ |
|
|
656 |
easing: 'linear', |
|
|
657 |
left: '-119px', |
|
|
658 |
duration: 2 |
|
|
659 |
}, function () { // when the row finishes its right to left transition... |
|
|
660 |
// remove the first duck on the left |
|
|
661 |
// which has trasitioned out of view |
|
|
662 |
frontDuck = Y.one('.duck-row li').remove(); |
|
|
663 |
|
|
|
664 |
// append the removed first duck onto the right end |
|
|
665 |
Y.one('.duck-row').appendChild(frontDuck); |
|
|
666 |
|
|
|
667 |
// set the position for the next makeDucksSwim() |
|
|
668 |
Y.one('.duck-row').setStyle('left', '10px'); |
|
|
669 |
|
|
|
670 |
// if there are ducks remaining, make them swim again |
|
|
671 |
if (ducksRemaining > 0) { |
|
|
672 |
makeDucksSwim(); |
|
|
673 |
} |
|
|
674 |
}); |
|
|
675 |
} |
|
|
676 |
makeDucksSwim(); // this initializes the ducks swimming |
|
|
677 |
|
|
|
678 |
// handles a click on a duck |
|
|
679 |
var duckClick = function(e) { |
|
|
680 |
var squawks; |
|
|
681 |
|
|
|
682 |
// remove the squawk belonging to the duck that was clicked |
|
|
683 |
e.currentTarget.one('.squawk').setStyles({'top': '-400px', 'opacity': '1'}); |
|
|
684 |
|
|
|
685 |
// makes the ducks appear to lay back when clicked |
|
|
686 |
e.target.transition({ |
|
|
687 |
duration: 0.2, |
|
|
688 |
height: '3px', |
|
|
689 |
width: '133px' |
|
|
690 |
}); |
|
|
691 |
|
|
|
692 |
// the clicked duck will no longer have the 'set-up' class/state |
|
|
693 |
e.currentTarget.removeClass('set-up'); |
|
|
694 |
makeDucksSquawk(); |
|
|
695 |
updateDucksRemaining(); // update the number of ducks still set up |
|
|
696 |
}; |
|
|
697 |
|
|
|
698 |
// this makes the duck's squawks show and hide and get various text |
|
|
699 |
var makeDucksSquawk = function(){ |
|
|
700 |
squawks = Y.all('.duck-row .set-up .squawk'); // a NodeList of the squawks of set-up ducks |
|
|
701 |
if (Y.one('#show-attitude')._node.checked) { // only have ducks squawk if the checkbox is checked |
|
|
702 |
// fill voice bubbles with next text string |
|
|
703 |
Y.all('.duck-row .set-up .squawk .text').setHTML(squawkTextArr[squawkTextIndex]); |
|
|
704 |
|
|
|
705 |
// increment the index to get the next squawk text |
|
|
706 |
squawkTextIndex = (squawkTextIndex += 1) % (squawkTextArr.length); |
|
|
707 |
squawks.transition({ |
|
|
708 |
top: { |
|
|
709 |
delay: 0.5, |
|
|
710 |
value: '0px', // drop squawks into position from hidden |
|
|
711 |
duration: 0 // instant position change |
|
|
712 |
}, |
|
|
713 |
opacity: { // fade out |
|
|
714 |
delay: 3.0, |
|
|
715 |
duration: 0.3, |
|
|
716 |
value: 0 |
|
|
717 |
} |
|
|
718 |
}, function(e){ |
|
|
719 |
// after squawks are faded out, |
|
|
720 |
// move them to hidden position and set opacity to 1 again |
|
|
721 |
squawks.setStyles({'top': '-400px', 'opacity': '1'}); |
|
|
722 |
}); |
|
|
723 |
} |
|
|
724 |
} |
|
|
725 |
|
|
|
726 |
// This resets all ducks, "ducks remaining" counters, and row position |
|
|
727 |
// make the duck images full height |
|
|
728 |
// start them swimming |
|
|
729 |
var reset = function() { |
|
|
730 |
Y.all('.duck-row li img').setStyle('height', '55px'); |
|
|
731 |
Y.all('.duck-row li').addClass('set-up'); |
|
|
732 |
updateDucksRemaining(); |
|
|
733 |
makeDucksSwim(); |
|
|
734 |
} |
|
|
735 |
|
|
|
736 |
// counts the ducks remaining, and updates the UI counter display |
|
|
737 |
var updateDucksRemaining = function() { |
|
|
738 |
ducksRemaining = Y.all('.gallery li.set-up').size(); |
|
|
739 |
Y.one('.ducks-remain').setHTML(ducksRemaining); |
|
|
740 |
} |
|
|
741 |
|
|
|
742 |
// listeners |
|
|
743 |
Y.one('.duck-row').delegate('click', duckClick, 'li'); |
|
|
744 |
Y.one('#button-reset').on('click', reset); |
|
|
745 |
}); |
|
|
746 |
</script></pre> |
|
|
747 |
|
|
|
748 |
</div> |
|
|
749 |
</div> |
|
|
750 |
</div> |
|
|
751 |
|
|
|
752 |
<div class="yui3-u-1-4"> |
|
|
753 |
<div class="sidebar"> |
|
|
754 |
|
|
|
755 |
|
|
|
756 |
|
|
|
757 |
<div class="sidebox"> |
|
|
758 |
<div class="hd"> |
|
|
759 |
<h2 class="no-toc">Examples</h2> |
|
|
760 |
</div> |
|
|
761 |
|
|
|
762 |
<div class="bd"> |
|
|
763 |
<ul class="examples"> |
|
|
764 |
|
|
|
765 |
|
|
|
766 |
<li data-description="Using selectors and property accessors with Node."> |
|
|
767 |
<a href="properties.html">Set and Get Properties</a> |
|
|
768 |
</li> |
|
|
769 |
|
|
|
770 |
|
|
|
771 |
|
|
|
772 |
<li data-description="Using DOM methods with Node."> |
|
|
773 |
<a href="dom-node.html">DOM Methods</a> |
|
|
774 |
</li> |
|
|
775 |
|
|
|
776 |
|
|
|
777 |
|
|
|
778 |
<li data-description="Building a simple store and shopping cart."> |
|
|
779 |
<a href="store.html">DOM Methods - Store</a> |
|
|
780 |
</li> |
|
|
781 |
|
|
|
782 |
|
|
|
783 |
|
|
|
784 |
<li data-description="Listening for DOM events with Node instances."> |
|
|
785 |
<a href="events.html">Handling DOM Events</a> |
|
|
786 |
</li> |
|
|
787 |
|
|
|
788 |
|
|
|
789 |
|
|
|
790 |
<li data-description="NodeList provides Node functionality for manipulating multiple nodes at once."> |
|
|
791 |
<a href="nodelist.html">Using NodeList - Simple</a> |
|
|
792 |
</li> |
|
|
793 |
|
|
|
794 |
|
|
|
795 |
|
|
|
796 |
<li data-description="How to use multiple NodeList features to build a simple game."> |
|
|
797 |
<a href="ducks.html">Using NodeList - Ducks Game</a> |
|
|
798 |
</li> |
|
|
799 |
|
|
|
800 |
|
|
|
801 |
|
|
|
802 |
<li data-description="Using a single event listener to handle events on multiple nodes."> |
|
|
803 |
<a href="node-evt-delegation.html">Delegating Node Events</a> |
|
|
804 |
</li> |
|
|
805 |
|
|
|
806 |
|
|
|
807 |
|
|
|
808 |
<li data-description="This example demonstrates how to position an element in page coordinates."> |
|
|
809 |
<a href="node-xy.html">Node Positioning</a> |
|
|
810 |
</li> |
|
|
811 |
|
|
|
812 |
|
|
|
813 |
|
|
|
814 |
<li data-description="This example demonstrates how to set styles and get style information."> |
|
|
815 |
<a href="node-style.html">Node Styling</a> |
|
|
816 |
</li> |
|
|
817 |
|
|
|
818 |
|
|
|
819 |
|
|
|
820 |
<li data-description="This example demonstrates how to insert content into a Node."> |
|
|
821 |
<a href="node-insert.html">Adding Node Content - Burger Builder</a> |
|
|
822 |
</li> |
|
|
823 |
|
|
|
824 |
|
|
|
825 |
|
|
|
826 |
<li data-description="This example demonstrates how to show and hide a Node."> |
|
|
827 |
<a href="node-view.html">Showing and Hiding</a> |
|
|
828 |
</li> |
|
|
829 |
|
|
|
830 |
|
|
|
831 |
|
|
|
832 |
|
|
|
833 |
|
|
|
834 |
|
|
|
835 |
|
|
|
836 |
|
|
|
837 |
|
|
|
838 |
|
|
|
839 |
|
|
|
840 |
|
|
|
841 |
|
|
|
842 |
|
|
|
843 |
|
|
|
844 |
|
|
|
845 |
</ul> |
|
|
846 |
</div> |
|
|
847 |
</div> |
|
|
848 |
|
|
|
849 |
|
|
|
850 |
|
|
|
851 |
<div class="sidebox"> |
|
|
852 |
<div class="hd"> |
|
|
853 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
854 |
</div> |
|
|
855 |
|
|
|
856 |
<div class="bd"> |
|
|
857 |
<ul class="examples"> |
|
|
858 |
|
|
|
859 |
|
|
|
860 |
|
|
|
861 |
|
|
|
862 |
|
|
|
863 |
|
|
|
864 |
|
|
|
865 |
|
|
|
866 |
|
|
|
867 |
|
|
|
868 |
|
|
|
869 |
|
|
|
870 |
|
|
|
871 |
|
|
|
872 |
|
|
|
873 |
|
|
|
874 |
|
|
|
875 |
|
|
|
876 |
|
|
|
877 |
|
|
|
878 |
|
|
|
879 |
|
|
|
880 |
|
|
|
881 |
|
|
|
882 |
<li data-description="Creating an accessible toolbar using the Focus Manager Node Plugin and Node's support for the WAI-ARIA Roles and States."> |
|
|
883 |
<a href="../node-focusmanager/node-focusmanager-toolbar.html">Accessible Toolbar</a> |
|
|
884 |
</li> |
|
|
885 |
|
|
|
886 |
|
|
|
887 |
|
|
|
888 |
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States."> |
|
|
889 |
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a> |
|
|
890 |
</li> |
|
|
891 |
|
|
|
892 |
|
|
|
893 |
|
|
|
894 |
<li data-description="Use the Event Utility to attach simple DOM event handlers."> |
|
|
895 |
<a href="../event/basic-example.html">Simple DOM Events</a> |
|
|
896 |
</li> |
|
|
897 |
|
|
|
898 |
|
|
|
899 |
|
|
|
900 |
<li data-description="Example Photo Browser application."> |
|
|
901 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
902 |
</li> |
|
|
903 |
|
|
|
904 |
|
|
|
905 |
|
|
|
906 |
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
|
907 |
<a href="../dd/portal-drag.html">Portal Style Example</a> |
|
|
908 |
</li> |
|
|
909 |
|
|
|
910 |
|
|
|
911 |
|
|
|
912 |
<li data-description="Use IO to request XML data from a remote web service."> |
|
|
913 |
<a href="../io/weather.html">Request XML data from Yahoo! Weather</a> |
|
|
914 |
</li> |
|
|
915 |
|
|
|
916 |
|
|
|
917 |
|
|
|
918 |
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources."> |
|
|
919 |
<a href="../io/xdr.html">Request JSON using Yahoo! Pipes</a> |
|
|
920 |
</li> |
|
|
921 |
|
|
|
922 |
|
|
|
923 |
</ul> |
|
|
924 |
</div> |
|
|
925 |
</div> |
|
|
926 |
|
|
|
927 |
</div> |
|
|
928 |
</div> |
|
|
929 |
</div> |
|
|
930 |
</div> |
|
|
931 |
|
|
|
932 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
933 |
<script>prettyPrint();</script> |
|
|
934 |
|
|
|
935 |
<script> |
|
|
936 |
YUI.Env.Tests = { |
|
|
937 |
examples: [], |
|
|
938 |
project: '../assets', |
|
|
939 |
assets: '../assets/node', |
|
|
940 |
name: 'ducks', |
|
|
941 |
title: 'Using NodeList - Ducks Game', |
|
|
942 |
newWindow: '', |
|
|
943 |
auto: false |
|
|
944 |
}; |
|
|
945 |
YUI.Env.Tests.examples.push('properties'); |
|
|
946 |
YUI.Env.Tests.examples.push('dom-node'); |
|
|
947 |
YUI.Env.Tests.examples.push('store'); |
|
|
948 |
YUI.Env.Tests.examples.push('events'); |
|
|
949 |
YUI.Env.Tests.examples.push('nodelist'); |
|
|
950 |
YUI.Env.Tests.examples.push('ducks'); |
|
|
951 |
YUI.Env.Tests.examples.push('node-evt-delegation'); |
|
|
952 |
YUI.Env.Tests.examples.push('node-xy'); |
|
|
953 |
YUI.Env.Tests.examples.push('node-style'); |
|
|
954 |
YUI.Env.Tests.examples.push('node-insert'); |
|
|
955 |
YUI.Env.Tests.examples.push('node-view'); |
|
|
956 |
YUI.Env.Tests.examples.push('node-focusmanager-toolbar'); |
|
|
957 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
958 |
YUI.Env.Tests.examples.push('basic-example'); |
|
|
959 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
960 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
961 |
YUI.Env.Tests.examples.push('weather'); |
|
|
962 |
YUI.Env.Tests.examples.push('xdr'); |
|
|
963 |
|
|
|
964 |
</script> |
|
|
965 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
966 |
|
|
|
967 |
|
|
|
968 |
|
|
|
969 |
</body> |
|
|
970 |
</html> |