YUI 3: Drag & Drop (DD)
The Drag & Drop Utility allows you to create a draggable interface efficiently, buffering you from browser-level abnormalities and enabling you to focus on the interesting logic surrounding your particular implementation. This component enables you to create a variety of standard draggable objects with just a few lines of code and then, using its extensive API, add your own specific implementation logic.
For more information about drag and drop as a design pattern, see the Yahoo! Design Pattern Library.
More Information
- Examples: Drag & Drop Utility in action.
- API Documentation: View the full API documentation for the Drag & Drop Utility.
- Download: Drag & Drop Utility as part of the full YUI Library.
- Free Hosting on our fast edge servers with combo-loading.
- License: BSD.
Getting Started
Include Dependencies
The easiest way to include the source files for and it dependencies, is to add the YUI seed file to your page, using the following script tag, and allow the YUI instance to download any additional files which maybe required:
The YUI instance will automatically pull down 's source files, along
with any dependent files missing from the page, when the dd
module is used in your code. This helps you avoid having to manually manage the
list of files which need to be included on your page to support multiple components,
while also optimizing your initial page weight by loading files only when they are
required.
If you do want to include file dependencies manually on your page, the YUI Configurator (discussed below), can be used to determine the list of files you need to include in order to use .
The YUI Instance
Once you have the YUI seed file on your page (yui-min.js), you can
create a new YUI instance for your application to use, and populate it
with the modules you need, specified as the first set of arguments to the
use method:
The last argument passed to use is a callback function. The callback function will be
invoked as soon as the YUI instance is done downloading any required files missing from
your page, and populated the local YUI instance with the classes which make up
the dd module and any modules it depends on.
A reference to the populated YUI instance (Y) is passed back to the
callback function inside which you can start writing your application code based on your own, custom
instance of YUI.
NOTE: If you're not using the complete set of functionality the dd module delivers,
you can lighten your page weight by specifying only the modules you need to the use method from
the module list below.
For more information on creating instances of YUI and the
use method, see the
YUI Global object documentation.
Using the Drag & Drop Utility
This section describes how to use the Drag & Drop Utility in further detail. It contains these subsections:
Module Descriptions
Drag & Drop for YUI 3.0 has been broken up into several modules to allow you, as the implementer, to pick how you want it to work and what code you need on your page.
| Module Name | Description |
|---|---|
dd-ddm-base |
Base DragDrop Manager, required to make something draggable. |
dd-ddm |
Adds shim support, only needed when you need to drag over something that steals mouse events or you are targeting. |
dd-ddm-drop |
Adds Drop support, only required when you have drop targets you need to interact with. |
dd-drag |
Main drag class, this makes something draggable. |
dd-proxy |
Adds proxy support to the main drag class. |
dd-constrain |
Adds constrain support to the main drag class. |
dd-scroll |
Adds node and window based scroll support. |
dd-drop |
Drop Support, this is the support for all drop targets. |
dd-plugin |
Node plugin for Drag |
dd-drop-plugin |
Node plugin for Drop |
dd |
All of the above modules rolled up into one file. |
Simple Drag & Drop
Basic Drag
You create a simple Drag instance by including the dd-drag module and using the following code:
Basic Proxy Drag
You create a simple Proxy Drag instance by including the dd-drag and dd-proxy modules and using the following code:
Basic Drop Target
You create a simple Drop Target instance by including the dd-drop module and using the following code:
Events
Drag & Drop for YUI 3.0 has been been packed with useful events to allow the implementer to control the end user experience.
| Event | Target | Preventable | Stoppable | Bubbles | Description |
|---|---|---|---|---|---|
drag:mouseDown |
Drag | yes | yes | yes | Handles the mousedown DOM event, checks to see if you have a valid handle then starts the drag timers. |
drag:afterMouseDown |
Drag | no | no | yes | Fires after the mousedown event has been cleared. |
drag:align |
Drag | yes | yes | yes | Fires when this node is aligned. |
drag:removeHandle |
Drag | no | no | yes | Fires after a handle is removed. |
drag:addHandle |
Drag | no | no | yes | Fires after a handle is added. |
drag:removeInvalid |
Drag | no | no | yes | Fires after an invalid selector is removed. |
drag:addInvalid |
Drag | no | no | yes | Fires after an invalid selector is added. |
drag:start |
Drag | no | no | yes | Fires at the start of a drag operation. |
drag:end |
Drag | yes | yes | yes | Fires at the end of a drag operation. |
drag:drag |
Drag | yes | yes | yes | Fires every mousemove during a drag operation. |
drag:over |
Drag | no | no | yes | Fires when this node is over a Drop Target. (Fired from dd-drop) |
drag:enter |
Drag | no | no | yes | Fires when this node enters a Drop Target. (Fired from dd-drop) |
drag:exit |
Drag | no | no | yes | Fires when this node exits a Drop Target. (Fired from dd-drop) |
drag:drophit |
Drag | no | no | yes | Fires when this node is dropped on a valid Drop Target. (Fired from dd-ddm-drop) |
drag:dropmiss |
Drag | no | no | yes | Fires when this node is dropped on an invalid Drop Target. (Fired from dd-ddm-drop) |
drop:over |
Drop | no | no | yes | Fires when a drag element is over this target. |
drop:enter |
Drop | no | no | yes | Fires when a drag element enters this target. |
drop:exit |
Drop | no | no | yes | Fires when a drag element exits this target. |
drop:hit |
Drop | no | no | yes | Fires when a draggable node is dropped on this Drop Target. (Fired from dd-ddm-drop) |
Event Bubbling
To allow for a truly Event driven application, all Drag & Drop related events are bubbled to the DragDropMgr.
Per YUI instance, you can listen for all Drag & Drop events from a central location.
So instead of doing this:
You can now do this:
CSS Class Names
The Drag & Drop Utility adds CSS class names for important moments in the drag and drop operation. Below you will find the list of these class names.
The Drag & Drop Utility doesn't ship with a default skin, so no style rules are attached to these class names. That is completely left up to the implementer.
| Class Name | Target | State |
|---|---|---|
yui-dd-draggable |
Drag | Given to all Drag Nodes |
yui-dd-locked |
Drag | Added when a Drag instance is locked |
yui-dd-dragging |
Drag | Added while a Drag instance is active |
yui-dd-proxy |
Proxy | Given to the Proxy Node |
yui-dd-drop |
Drop | Given to all Drop Targets |
yui-dd-drop-locked |
Drop | Added when a Drop instance is locked |
yui-dd-drop-active-valid |
Drop | Added to a Drop when it is an valid target for the current drag operation |
yui-dd-drop-active-invalid |
Drop | Added to a Drop when it is an invalid target for the current drag operation |
yui-dd-drop-over |
Drop | Added when a Drag instance is over this Drop Target |
Support & Community
Forums & Blog
YUI 3 discussion forums are hosted on YUILibrary.com.
In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.
Filing Bugs & Feature Requests
The YUI Library's public bug tracking and feature request repositories are located on the YUILibrary.com site. Before filing new feature requests or bug reports, please review our reporting guidelines.

