4
<title>jQuery UI Draggable - Constrain movement</title>
5
<link type="text/css" href="../../themes/base/ui.all.css" rel="stylesheet" />
6
<script type="text/javascript" src="../../jquery-1.3.2.js"></script>
7
<script type="text/javascript" src="../../ui/ui.core.js"></script>
8
<script type="text/javascript" src="../../ui/ui.draggable.js"></script>
9
<link type="text/css" href="../demos.css" rel="stylesheet" />
10
<style type="text/css">
11
.draggable { width: 90px; height: 90px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
12
#draggable, #draggable2 { margin-bottom:20px; }
13
#draggable { cursor: n-resize; }
14
#draggable2 { cursor: e-resize; }
15
#containment-wrapper { width: 95%; height:150px; border:2px solid #ccc; padding: 10px;}
17
<script type="text/javascript">
19
$("#draggable").draggable({ axis: 'y' });
20
$("#draggable2").draggable({ axis: 'x' });
22
$("#draggable3").draggable({ containment: '#containment-wrapper', scroll: false });
23
$("#draggable4").draggable({ containment: '#demo-frame' });
24
$("#draggable5").draggable({ containment: 'parent' });
32
<h3 class="docs">Constrain movement along an axis:</h3>
34
<div id="draggable" class="draggable ui-widget-content">
35
<p>I can be dragged only vertically</p>
38
<div id="draggable2" class="draggable ui-widget-content">
39
<p>I can be dragged only horizontally</p>
42
<h3 class="docs">Or to within another DOM element:</h3>
43
<div id="containment-wrapper">
44
<div id="draggable3" class="draggable ui-widget-content">
45
<p>I'm contained within the box</p>
48
<div id="draggable4" class="draggable ui-widget-content">
49
<p>I'm contained within the box's parent</p>
52
<div class="draggable ui-widget-content">
53
<p id="draggable5" class="ui-widget-header">I'm contained within my parent</p>
57
</div><!-- End demo -->
59
<div class="demo-description">
62
Constrain the movement of each draggable by defining the boundaries of the draggable area. Set the <code>axis</code> option to limit the draggable's path to the x- or y-axis, or use the <code>containment</code> option to specify a parent DOM element or a jQuery selector, like 'document.'
65
</div><!-- End demo-description -->