YUI Library Home

YUI Library Examples: Drag & Drop: Using a Proxy Element

Drag & Drop: Using a Proxy Element

This example demonstrates drag and drop using a proxy element.

Custom

Drag and Drop Proxies

The YUI Drag & Drop Utility lets you make HTML elements draggable.

For this example, we will enable drag and drop for the three <div> elements. However, instead of dragging the elements around, a proxy element is created and moved instead. Many implmentations can benefit from the use of the proxy element because it avoids positioning complications that can occur when changing the css styles of the source element. Proxy drags also perform better than dragging the element directly.

Create the demo elements:

1<div id="dd-demo-1" class="dd-demo"></div> 
2<div id="dd-demo-2" class="dd-demo"></div> 
3<div id="dd-demo-3" class="dd-demo"></div> 
4 
5<div id="dd-demo-3-proxy">Custom</div> 
view plain | print | ?

Now we create the instances of YAHOO.util.DDProxy, passing the element ids or references for our demo elements.

1<script type="text/javascript"
2    (function() { 
3        var dd, dd2, dd3; 
4        YAHOO.util.Event.onDOMReady(function() { 
5            // The first two instances will share a proxy 
6            // element, created automatically by the utility. 
7            // This element will be resized at drag time so 
8            // that it matches the size of the source element. 
9            // It is configured by default to have a 2 pixel 
10            // grey border. 
11            dd = new YAHOO.util.DDProxy("dd-demo-1"); 
12            dd2 = new YAHOO.util.DDProxy("dd-demo-2"); 
13 
14            // The third instance has a dedicated custom proxy 
15            dd3 = new YAHOO.util.DDProxy("dd-demo-3""default", {  
16 
17                    // Define a custom proxy element.  It will be 
18                    // created if not already on the page. 
19                    dragElId: "dd-demo-3-proxy",  
20 
21                    // When a drag starts, the proxy is normally 
22                    // resized.  Turn this off so we can keep a 
23                    // fixed sized proxy. 
24                    resizeFrame: false 
25                }); 
26 
27        }); 
28    })(); 
29</script> 
view plain | print | ?

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings