YUI recommends YUI3.
YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.
This documentation is no longer maintained.
This example shows how to keep draggable elements from being dragged out of a region.
The three elements below will not be able to be dragged beyond the borders of their own colored elements.
First we setup the HTML for the canvas (playing field) and place some players on it.
1 | <div id="dd-demo-canvas1"> |
2 | <div id="dd-demo-canvas2"> |
3 | <div id="dd-demo-canvas3"> |
4 | <div id="dd-demo-1" class="dd-demo"><div>1</div></div> |
5 | <div id="dd-demo-2" class="dd-demo"><div>2</div></div> |
6 | <div id="dd-demo-3" class="dd-demo"><div>3</div></div> |
7 | </div> |
8 | </div> |
9 | </div> |
view plain | print | ? |
Next we have to get the size of the playing field. To do this, we will use YAHOO.util.Dom.getRegion('dd-demo-canvas')
. getRegion will return something like this:
1 | { |
2 | 0: 255, |
3 | 1: 248, |
4 | bottom: 432, |
5 | left: 255, |
6 | right: 1068, |
7 | top: 248 |
8 | } |
view plain | print | ? |
Now we have the top, bottom, left and right pixel locations of the playing field, we can do some math and set some constraints.
The math for this is pretty easy, as shown in the code below:
It should be noted that the box model comes into play here. Placing padding, margins or borders on the playing field or the draggable elements will cause them to not stay fully in the playing field. They will overlap the region by the width of the margin/padding/border.
1 | //Get the top, right, bottom and left positions |
2 | var region = Dom.getRegion(this.cont); |
3 | |
4 | //Get the element we are working on |
5 | var el = this.getEl(); |
6 | |
7 | //Get the xy position of it |
8 | var xy = Dom.getXY(el); |
9 | |
10 | //Get the width and height |
11 | var width = parseInt(Dom.getStyle(el, 'width'), 10); |
12 | var height = parseInt(Dom.getStyle(el, 'height'), 10); |
13 | |
14 | //Set left to x minus left |
15 | var left = xy[0] - region.left; |
16 | |
17 | //Set right to right minus x minus width |
18 | var right = region.right - xy[0] - width; |
19 | |
20 | //Set top to y minus top |
21 | var top = xy[1] - region.top; |
22 | |
23 | //Set bottom to bottom minus y minus height |
24 | var bottom = region.bottom - xy[1] - height; |
view plain | print | ? |
Now that we have the vars for top, bottom, left and right, we can set contraints on a Drag Drop instance with setXConstraint
and setYConstraint
1 | (function() { |
2 | var Dom = YAHOO.util.Dom, |
3 | Event = YAHOO.util.Event, |
4 | dd1, dd2, dd3; |
5 | |
6 | |
7 | YAHOO.example.DDRegion = function(id, sGroup, config) { |
8 | this.cont = config.cont; |
9 | YAHOO.example.DDRegion.superclass.constructor.apply(this, arguments); |
10 | }; |
11 | |
12 | YAHOO.extend(YAHOO.example.DDRegion, YAHOO.util.DD, { |
13 | cont: null, |
14 | init: function() { |
15 | //Call the parent's init method |
16 | YAHOO.example.DDRegion.superclass.init.apply(this, arguments); |
17 | this.initConstraints(); |
18 | |
19 | Event.on(window, 'resize', function() { |
20 | this.initConstraints(); |
21 | }, this, true); |
22 | }, |
23 | initConstraints: function() { |
24 | //Get the top, right, bottom and left positions |
25 | var region = Dom.getRegion(this.cont); |
26 | |
27 | //Get the element we are working on |
28 | var el = this.getEl(); |
29 | |
30 | //Get the xy position of it |
31 | var xy = Dom.getXY(el); |
32 | |
33 | //Get the width and height |
34 | var width = parseInt(Dom.getStyle(el, 'width'), 10); |
35 | var height = parseInt(Dom.getStyle(el, 'height'), 10); |
36 | |
37 | //Set left to x minus left |
38 | var left = xy[0] - region.left; |
39 | |
40 | //Set right to right minus x minus width |
41 | var right = region.right - xy[0] - width; |
42 | |
43 | //Set top to y minus top |
44 | var top = xy[1] - region.top; |
45 | |
46 | //Set bottom to bottom minus y minus height |
47 | var bottom = region.bottom - xy[1] - height; |
48 | |
49 | //Set the constraints based on the above calculations |
50 | this.setXConstraint(left, right); |
51 | this.setYConstraint(top, bottom); |
52 | } |
53 | }); |
54 | |
55 | |
56 | Event.onDOMReady(function() { |
57 | dd1 = new YAHOO.example.DDRegion('dd-demo-1', '', { cont: 'dd-demo-canvas3' }); |
58 | dd2 = new YAHOO.example.DDRegion('dd-demo-2', '', { cont: 'dd-demo-canvas2' }); |
59 | dd3 = new YAHOO.example.DDRegion('dd-demo-3', '', { cont: 'dd-demo-canvas1' }); |
60 | }); |
61 | |
62 | })(); |
view plain | print | ? |
Note: Logging and debugging is currently turned off for this example.
All YUI 2.x users should review the YUI 2.8.2 security bulletin, which discusses a vulnerability present in YUI 2.4.0-2.8.1.
Copyright © 2013 Yahoo! Inc. All rights reserved.