<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example: Drag - Constrained to a Region</title>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
<link rel="stylesheet" href="../assets/css/main.css">
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
<script src="../../build/yui/yui-min.js"></script>
</head>
<body>
<!--
<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>
-->
<div id="doc">
<div id="hd">
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
</div>
<a href="#toc" class="jump">Jump to Table of Contents</a>
<h1>Example: Drag - Constrained to a Region</h1>
<div class="yui3-g">
<div class="yui3-u-3-4">
<div id="main">
<div class="content"><div class="intro">
<p>This example shows how to constrain a draggable Node to another Node's region.</p>
</div>
<div class="example">
<style>
.dd-demo {
position: relative;
text-align: center;
color: #fff;
cursor: move;
height: 60px;
width: 60px;
padding: 0;
margin: 0;
}
.dd-demo div {
border: 1px solid black;
height: 58px;
width: 58px;
}
#dd-demo-canvas1 {
padding: 55px;
background-color: #004C6D;
border: 1px solid black;
}
#dd-demo-canvas2 {
padding: 25px;
background-color: #CDCDCD;
border: 1px solid black;
}
#dd-demo-canvas3 {
padding: 15px;
background-color: #8DD5E7;
border: 1px solid black;
}
#dd-demo-1 {
background-color: #8DD5E7;
top:5px;
left:5px;
color: #000;
}
#dd-demo-2 {
background-color: #CDCDCD;
top:50px;
left:100px;
color: #000;
}
#dd-demo-3 {
background-color: #004C6D;
top:-100px;
left:200px;
}
</style>
<div id="dd-demo-canvas1">
<div id="dd-demo-canvas2">
<div id="dd-demo-canvas3">
<div id="dd-demo-1" class="dd-demo"><div>1</div></div>
<div id="dd-demo-2" class="dd-demo"><div>2</div></div>
<div id="dd-demo-3" class="dd-demo"><div>3</div></div>
</div>
</div>
</div>
<script>
YUI().use('dd-constrain', function(Y) {
var dd1 = new Y.DD.Drag({
node: '#dd-demo-1'
}).plug(Y.Plugin.DDConstrained, {
constrain2node: '#dd-demo-canvas3'
});
var dd2 = new Y.DD.Drag({
node: '#dd-demo-2'
}).plug(Y.Plugin.DDConstrained, {
constrain2node: '#dd-demo-canvas2'
});
var dd3 = new Y.DD.Drag({
node: '#dd-demo-3'
}).plug(Y.Plugin.DDConstrained, {
constrain2node: '#dd-demo-canvas1'
});
});
</script>
</div>
<h3 id="setting-up-the-node">Setting up the Node</h3>
<p>First we need to create the HTML structure used in this example.</p>
<pre class="code prettyprint"><div id="dd-demo-canvas1">
<div id="dd-demo-canvas2">
<div id="dd-demo-canvas3">
<div id="dd-demo-1" class="dd-demo"><div>1</div></div>
<div id="dd-demo-2" class="dd-demo"><div>2</div></div>
<div id="dd-demo-3" class="dd-demo"><div>3</div></div>
</div>
</div>
</div></pre>
<p>Now we give the Nodes some CSS to make them visible.</p>
<pre class="code prettyprint">.dd-demo {
position: relative;
text-align: center;
color: #fff;
cursor: move;
height: 60px;
width: 60px;
padding: 0;
margin: 0;
}
.dd-demo div {
border: 1px solid black;
height: 58px;
width: 58px;
}
#dd-demo-canvas1 {
padding: 55px;
background-color: #004C6D;
border: 1px solid black;
}
#dd-demo-canvas2 {
padding: 25px;
background-color: #CDCDCD;
border: 1px solid black;
}
#dd-demo-canvas3 {
padding: 15px;
background-color: #8DD5E7;
border: 1px solid black;
}
#dd-demo-1 {
background-color: #8DD5E7;
top:5px;
left:5px;
color: #000;
}
#dd-demo-2 {
background-color: #CDCDCD;
top:50px;
left:100px;
color: #000;
}
#dd-demo-3 {
background-color: #004C6D;
top:-100px;
left:200px;
}</pre>
<h3 id="setting-up-the-yui-instance">Setting up the YUI Instance</h3>
<p>Now we need to create our YUI instance and tell it to load the <code>dd-constrain</code> module (that will load the dd-ddm and dd-drag modules too).</p>
<pre class="code prettyprint">YUI().use('dd-constrain');</pre>
<h3 id="making-the-nodes-draggable">Making the Nodes draggable</h3>
<p>Now that we have a YUI instance with the <code>dd-constrain</code> module, we need to instantiate the <code>Drag</code> instance on the Nodes.</p>
<pre class="code prettyprint">YUI().use('dd-constrain', function(Y) {
var dd1 = new Y.DD.Drag({
node: '#dd-demo-1'
});
var dd2 = new Y.DD.Drag({
node: '#dd-demo-2'
});
var dd3 = new Y.DD.Drag({
node: '#dd-demo-3'
});
});</pre>
<h3 id="constrain-the-nodes-to-other-nodes">Constrain the Nodes to other Nodes</h3>
<p>Now that we have the Nodes draggable, we need to constrain them. We can do this by plugging the <code>DDConstrained</code> on to the <code>Drag</code> instance and passing it a config option called <code>constrain2node</code> and giving it a selector string of the Node we want to constrain to.</p>
<pre class="code prettyprint">YUI().use('dd-constrain', function(Y) {
var dd1 = new Y.DD.Drag({
node: '#dd-demo-1'
}).plug(Y.Plugin.DDConstrained, {
constrain2node: '#dd-demo-canvas3'
});
var dd2 = new Y.DD.Drag({
node: '#dd-demo-2'
}).plug(Y.Plugin.DDConstrained, {
constrain2node: '#dd-demo-canvas2'
});
var dd3 = new Y.DD.Drag({
node: '#dd-demo-3'
}).plug(Y.Plugin.DDConstrained, {
constrain2node: '#dd-demo-canvas1'
});
});</pre>
</div>
</div>
</div>
<div class="yui3-u-1-4">
<div class="sidebar">
<div id="toc" class="sidebox">
<div class="hd">
<h2 class="no-toc">Table of Contents</h2>
</div>
<div class="bd">
<ul class="toc">
<li>
<a href="#setting-up-the-node">Setting up the Node</a>
</li>
<li>
<a href="#setting-up-the-yui-instance">Setting up the YUI Instance</a>
</li>
<li>
<a href="#making-the-nodes-draggable">Making the Nodes draggable</a>
</li>
<li>
<a href="#constrain-the-nodes-to-other-nodes">Constrain the Nodes to other Nodes</a>
</li>
</ul>
</div>
</div>
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="A simple drag interaction that doesn't require a drop interaction.">
<a href="simple-drag.html">Simple Drag</a>
</li>
<li data-description="How to apply the Drag Plugin to a node.">
<a href="drag-plugin.html">Drag - Node plugin</a>
</li>
<li data-description="A simple proxy drag interaction that doesn't require a drop interaction.">
<a href="proxy-drag.html">Drag - Proxy</a>
</li>
<li data-description="How to constrain a draggable Node to another Node's region.">
<a href="constrained-drag.html">Drag - Constrained to a Region</a>
</li>
<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.">
<a href="groups-drag.html">Drag - Interaction Groups</a>
</li>
<li data-description="The use of the drag shim when dragging nodes over other troublesome nodes.">
<a href="shim-drag.html">Using the Drag Shim</a>
</li>
<li data-description="How to use the Drop Target events to code your application.">
<a href="drop-code.html">Using Drop Based Coding</a>
</li>
<li data-description="How you can use the DD Scroll plugin to scroll the browser window as you drag.">
<a href="winscroll.html">Window Scrolling</a>
</li>
<li data-description="How to use DD.Delegate to create a scalable solution which supports multiple draggable items.">
<a href="delegate.html">Drag Delegation</a>
</li>
<li data-description="Using DD.Delegate to support dragging multiple items and dropping them onto a Drop Target.">
<a href="delegate-drop.html">Drag Delegation with a Drop Target</a>
</li>
<li data-description="How to use Drag plugins with a DD Delegate based solution.">
<a href="delegate-plugins.html">Using Drag Plugins with Delegate</a>
</li>
<li data-description="This example shows how to make a sortable list using Custom Event Bubbling.">
<a href="list-drag.html">List Reorder w/Bubbling</a>
</li>
<li data-description="This example shows how to make a sortable list using Custom Event Bubbling and Node Scrolling.">
<a href="scroll-list.html">List Reorder w/Scrolling</a>
</li>
<li data-description="How to make an animated node a Drop target.">
<a href="anim-drop.html">Animated Drop Targets</a>
</li>
<li data-description="Example Photo Browser application.">
<a href="photo-browser.html">Photo Browser</a>
</li>
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
<a href="portal-drag.html">Portal Style Example</a>
</li>
</ul>
</div>
</div>
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples That Use This Component</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="Working with multiple YUI instances.">
<a href="../yui/yui-multi.html">Multiple Instances</a>
</li>
<li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
<a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script>
YUI.Env.Tests = {
examples: [],
project: '../assets',
assets: '../assets/dd',
name: 'constrained-drag',
title: 'Drag - Constrained to a Region',
newWindow: '',
auto: false
};
YUI.Env.Tests.examples.push('simple-drag');
YUI.Env.Tests.examples.push('drag-plugin');
YUI.Env.Tests.examples.push('proxy-drag');
YUI.Env.Tests.examples.push('constrained-drag');
YUI.Env.Tests.examples.push('groups-drag');
YUI.Env.Tests.examples.push('shim-drag');
YUI.Env.Tests.examples.push('drop-code');
YUI.Env.Tests.examples.push('winscroll');
YUI.Env.Tests.examples.push('delegate');
YUI.Env.Tests.examples.push('delegate-drop');
YUI.Env.Tests.examples.push('delegate-plugins');
YUI.Env.Tests.examples.push('list-drag');
YUI.Env.Tests.examples.push('scroll-list');
YUI.Env.Tests.examples.push('anim-drop');
YUI.Env.Tests.examples.push('photo-browser');
YUI.Env.Tests.examples.push('portal-drag');
YUI.Env.Tests.examples.push('yui-multi');
YUI.Env.Tests.examples.push('stylesheet-theme');
</script>
<script src="../assets/yui/test-runner.js"></script>
</body>
</html>