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.

YUI Library Examples: Drag & Drop: Staying in a Region

Drag & Drop: Staying in a Region

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.

1
2
3

Setting up the playing field

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 | ?

Size of the playing field

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

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 
2var region = Dom.getRegion(this.cont); 
3 
4//Get the element we are working on 
5var el = this.getEl(); 
6 
7//Get the xy position of it 
8var xy = Dom.getXY(el); 
9 
10//Get the width and height 
11var width = parseInt(Dom.getStyle(el, 'width'), 10); 
12var height = parseInt(Dom.getStyle(el, 'height'), 10); 
13 
14//Set left to x minus left 
15var left = xy[0] - region.left; 
16 
17//Set right to right minus x minus width 
18var right = region.right - xy[0] - width; 
19 
20//Set top to y minus top 
21var top = xy[1] - region.top; 
22 
23//Set bottom to bottom minus y minus height 
24var 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

Full Javascript Source

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            }, thistrue); 
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 | ?

Copyright © 2013 Yahoo! Inc. All rights reserved.

Privacy Policy - Copyright Policy - Job Openings