5
<title>Example: Animated Drop Targets</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: Animated Drop Targets</h1>
18
<a href="#toc" class="jump">Jump to Table of Contents</a>
22
<div class="yui3-u-3-4">
24
<div class="content"><div class="intro">
25
<p>This example will show you how to make an animated node a Drop target.</p>
28
<div class="example newwindow">
29
<a href="anim-drop-example.html" target="_blank" class="button">
30
View Example in New Window
36
<h3 id="setting-up-the-html">Setting up the HTML</h3>
37
<p>First we will create our HTML.</p>
39
<pre class="code prettyprint"><div id="dock"></div>
41
<div id="drag">Drag #1</div>
43
<div id="anim1" class="anim">Anim #1</div>
44
<div id="anim2" class="anim">Anim #2</div>
45
<div id="anim3" class="anim">Anim #3</div>
46
<div id="anim4" class="anim">Anim #4</div>
47
<div id="anim5" class="anim">Anim #5</div></pre>
50
<p>Now we give that HTML some CSS to make it visible.</p>
52
<pre class="code prettyprint">.anim {
56
border: 1px solid black;
57
background-color: #00B8BF;
63
border: 1px solid black;
64
background-color: #004C6D;
72
background-color: #D00050;
73
border: 1px solid black;
78
.anim.yui3-dd-drop-over {
79
background-color: #EDFF9F;
82
background-color: white;
84
#drag1.yui3-dd-drag-over {
86
filter: alpha(opacity=50);
90
<h3 id="setting-up-the-yui-instance">Setting up the YUI Instance</h3>
91
<p>Now we need to create our YUI instance and tell it to load the <code>dd-drop</code>, <code>dd-plugin</code>, <code>dd-drop-plugin</code> and <code>anim</code> modules.</p>
93
<pre class="code prettyprint">YUI().use('dd-drop', 'anim', 'dd-plugin', 'dd-drop-plugin');</pre>
96
<h3 id="making-the-node-draggable">Making the Node draggable</h3>
97
<p>Now that we have a YUI instance with the modules loaded, we need to instantiate the <code>Drag</code> instance on this Node.</p>
98
<p>In this example we will be using Node plugins to accomplish our tasks.</p>
100
<pre class="code prettyprint">YUI().use('dd-drop', 'anim', 'dd-plugin', 'dd-drop-plugin', function(Y) {
101
//Get the node #drag
102
var d = Y.one('#drag');
103
d.plug(Y.Plugin.Drag, { dragMode: 'intersect' });
107
<h3 id="animating-the-nodes">Animating the Nodes</h3>
108
<p>Now we will set up the Animation sequence that we want to run.</p>
110
<pre class="code prettyprint">//Get all the div's with the class anim
111
var anims = Y.Node.all('div.anim');
113
anims.each(function(v, k, items) {
114
//Get a reference to the Node instance
117
//Add the FX plugin
118
a.plug(Y.Plugin.NodeFX);
119
//Add the Drop plugin
120
a.plug(Y.Plugin.Drop);
122
//Set the attributes on the animation
132
for (var i = 0; i < n; ++i) {
134
Math.floor(Math.random()*Y.DOM.winWidth() - 60 - a.get('offsetWidth')),
135
Math.floor(Math.random()*Y.DOM.winHeight() - a.get('offsetHeight'))
141
//Do the animation 20 times
143
//Alternate it so it "bounces" across the screen
144
direction: 'alternate',
145
//Give all of them a different duration so we get different speeds.
146
duration: ((counter * 1.75) + 1)
151
<h3 id="making-the-node-a-target">Making the Node a Target</h3>
152
<p>Using the <code>dd-drop-plugin</code>, we now need to make this animated Node a Drop Target.</p>
153
<p>To do that, we need to add the plugin after the fx plugin.</p>
155
<pre class="code prettyprint">//Add the FX plugin
156
a.plug(Y.Plugin.NodeFX);
157
//Add the Drop plugin
158
a.plug(Y.Plugin.Drop);</pre>
161
<h3 id="syncing-the-target-with-the-animation">Syncing the Target with the Animation</h3>
162
<p>The Drop Target needs to be in sync with the animation, since we are changing the height, width, top and left style.</p>
163
<p>We do this by adding a listener to the <code>tween</code> event on the animation instance.</p>
165
<pre class="code prettyprint">//on tween of the original anim, we need to sync the drop's shim.
166
a.fx.on('tween', function() {
167
//Do we have an active Drag?
168
if (Y.DD.DDM.activeDrag) {
169
//Size this shim
170
this.drop.sizeShim();
171
//Force an over target check since we might not be moving the mouse.
172
Y.Lang.later(0, a, function() {
173
this.drop._handleTargetOver();
179
<h3 id="full-example-source">Full example source</h3>
181
<pre class="code prettyprint">YUI().use('dd', 'dd-plugin', 'dd-drop-plugin', 'anim', function(Y) {
182
//Get the node #drag
183
var d = Y.one('#drag');
184
d.plug(Y.Plugin.Drag, { dragMode: 'intersect' });
186
//Get all the divs with the class anim
187
var anims = Y.Node.all('div.anim');
189
anims.each(function(v, k) {
190
//Get a reference to the Node instance
193
//Add the FX plugin
194
a.plug(Y.Plugin.NodeFX);
195
//Add the Drop plugin
196
a.plug(Y.Plugin.Drop);
198
//Set the attributes on the animation
208
for (var i = 0; i < n; ++i) {
210
Math.floor(Math.random()*Y.DOM.winWidth() - 60 - a.get('offsetWidth')),
211
Math.floor(Math.random()*Y.DOM.winHeight() - a.get('offsetHeight'))
217
//Do the animation 20 times
219
//Alternate it so it "bounces" across the screen
220
direction: 'alternate',
221
//Give all of them a different duration so we get different speeds.
222
duration: ((counter * 1.75) + 1)
225
//When this drop is entered, pause the fx
226
a.drop.on('drop:enter', function() {
229
//When the drop is exited, run the fx again
230
a.drop.on('drop:exit', function() {
233
//When a drop is on the node, do something special
234
a.drop.on('drop:hit', function(e) {
235
//Stop the animation
237
//remove the tween listener
238
this.fx.unsubscribeAll('tween');
239
//move it to the dock
248
var dW = Y.one('body').get('viewportRegion').right;
249
return ((dW - 60)); //Minus 60 for the dock
254
direction: 'normal',
257
//We are using reverse above for the "bouncing", reset it here.
261
//On end, add a class and destroy the target
262
this.fx.on('end', function() {
263
this.drop.get('node').addClass('done');
266
//Run this animation
269
//others that were dropped on.
270
Y.each(e.others, function(v, k) {
271
var node = v.get('node');
277
//on tween of the original anim, we need to sync the drop's shim.
278
a.fx.on('tween', function() {
279
//Do we have an active Drag?
280
if (Y.DD.DDM.activeDrag) {
281
//Size this shim
282
this.drop.sizeShim();
283
//Force an over target check since we might not be moving the mouse.
284
Y.Lang.later(0, a, function() {
285
this.drop._handleTargetOver();
298
<div class="yui3-u-1-4">
299
<div class="sidebar">
301
<div id="toc" class="sidebox">
303
<h2 class="no-toc">Table of Contents</h2>
309
<a href="#setting-up-the-html">Setting up the HTML</a>
312
<a href="#setting-up-the-yui-instance">Setting up the YUI Instance</a>
315
<a href="#making-the-node-draggable">Making the Node draggable</a>
318
<a href="#animating-the-nodes">Animating the Nodes</a>
321
<a href="#making-the-node-a-target">Making the Node a Target</a>
324
<a href="#syncing-the-target-with-the-animation">Syncing the Target with the Animation</a>
327
<a href="#full-example-source">Full example source</a>
335
<div class="sidebox">
337
<h2 class="no-toc">Examples</h2>
341
<ul class="examples">
344
<li data-description="A simple drag interaction that doesn't require a drop interaction.">
345
<a href="simple-drag.html">Simple Drag</a>
350
<li data-description="How to apply the Drag Plugin to a node.">
351
<a href="drag-plugin.html">Drag - Node plugin</a>
356
<li data-description="A simple proxy drag interaction that doesn't require a drop interaction.">
357
<a href="proxy-drag.html">Drag - Proxy</a>
362
<li data-description="How to constrain a draggable Node to another Node's region.">
363
<a href="constrained-drag.html">Drag - Constrained to a Region</a>
368
<li data-description="Using interaction groups, this example demonstrates how to tie into the Drag & Drop Utility's interesting moments to provide visual affordances for the current drag operation.">
369
<a href="groups-drag.html">Drag - Interaction Groups</a>
374
<li data-description="The use of the drag shim when dragging nodes over other troublesome nodes.">
375
<a href="shim-drag.html">Using the Drag Shim</a>
380
<li data-description="How to use the Drop Target events to code your application.">
381
<a href="drop-code.html">Using Drop Based Coding</a>
386
<li data-description="How you can use the DD Scroll plugin to scroll the browser window as you drag.">
387
<a href="winscroll.html">Window Scrolling</a>
392
<li data-description="How to use DD.Delegate to create a scalable solution which supports multiple draggable items.">
393
<a href="delegate.html">Drag Delegation</a>
398
<li data-description="Using DD.Delegate to support dragging multiple items and dropping them onto a Drop Target.">
399
<a href="delegate-drop.html">Drag Delegation with a Drop Target</a>
404
<li data-description="How to use Drag plugins with a DD Delegate based solution.">
405
<a href="delegate-plugins.html">Using Drag Plugins with Delegate</a>
410
<li data-description="This example shows how to make a sortable list using Custom Event Bubbling.">
411
<a href="list-drag.html">List Reorder w/Bubbling</a>
416
<li data-description="This example shows how to make a sortable list using Custom Event Bubbling and Node Scrolling.">
417
<a href="scroll-list.html">List Reorder w/Scrolling</a>
422
<li data-description="How to make an animated node a Drop target.">
423
<a href="anim-drop.html">Animated Drop Targets</a>
428
<li data-description="Example Photo Browser application.">
429
<a href="photo-browser.html">Photo Browser</a>
434
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
435
<a href="portal-drag.html">Portal Style Example</a>
449
<div class="sidebox">
451
<h2 class="no-toc">Examples That Use This Component</h2>
455
<ul class="examples">
490
<li data-description="Working with multiple YUI instances.">
491
<a href="../yui/yui-multi.html">Multiple Instances</a>
496
<li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
497
<a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
510
<script src="../assets/vendor/prettify/prettify-min.js"></script>
511
<script>prettyPrint();</script>