The Yahoo! User Interface Library has a number of nice features such as drag & drop and animation. Often, when you are wanting to prototype an HTML page, it would be nice to be able to sprinkle in a dash of drag & drop or decorate elements with animation.
The YAHOO.proto.SimpleDD makes it easy to add drag and drop to an HTML page.
The following examples shows the progression of an HTML page from plain markup to full drag and drop with animated drop.
Here are some additional examples:
To use the SimpleDD component, include the source file for SimpleDD and its dependencies in your web page with the following script tags:
<script src="yahoo.js"></script>
<script src="event.js"></script>
<script src="container_core.js"></script>
<script src="dom.js"></script>
<script src="animation.js"></script>
<script src="dragdrop.js"></script>
<!-- Source file -->
<script src="proto.js"></script>
The SimpleDD object is defined by YAHOO.proto.SimpleDD.
The most basic way to add drag & drop to the page is to tell the SimpleDD object what CSS class name marks the draggable items and which CSS class name marks the drop sites. The configuration parameters: dragClass and dropClass define these areas of the page.
var dd = new YAHOO.proto.SimpleDD("pictureDragger",
{
"dragClass": "dragme",
"dropClass": "drophere"
}
);
[html markup]
<div id="first-item" class="dragme">First Item</div>
<div id="second-item" class="dragme">Second Item</div>
<div id="Third-item" >Second Item</div>
<div id="drop-site" class="drophere">Drop It Here!</div>
The first parameter defines the name of this SimpleDD instance. It is used to define an interaction group. All items registered in the SimpleDD (both draggable and drop sites) will work together in the same interaction group. If you need a second set of drag items with different targets, then define a new SimpleDD with a different interaction group name.
In this example the DIVs first-item and second-item will be draggable because they share the class name dragme (passed to the SimpleDD constructor).
However, the DIV third-item will not be draggable since it does not have this class name.
Generally you will create a SimpleDD instance in the onload event for the document's body.
<script type="text/javascript">
var pageLoad = function()
{
var dd = new YAHOO.proto.SimpleDD("pictureDrag");
}
YAHOO.util.Event.addListener(window, 'load', pageLoad);
</script>
this.onMouseDownHandler = this.cfg.getProperty("onMouseDown");
this.startDragHandler = this.cfg.getProperty("startDrag");
this.onDragHandler = this.cfg.getProperty("onDrag");
this.onDragEnterHandler = this.cfg.getProperty("onDragEnter");
this.onDragOverHandler = this.cfg.getProperty("onDragOver");
this.onDragOutHandler = this.cfg.getProperty("onDragOut");
this.onDragDropHandler = this.cfg.getProperty("onDragDrop");
this.endDragHandler = this.cfg.getProperty("endDrag");
this.onMouseUpHandler = this.cfg.getProperty("onMouseUp");
When instantiating the SimpleDD object, the second parameter to the constructor is a YAHOO.Config object. Here are the attributes you may set:
| Property | Default Value | Description |
| dragClass | dragme | Any elements with this classname will be draggable. |
| dropClass | drophere | Any elements with this classname will be a valid drop site for the draggable items. |
| onMouseDown | null | The JavaScript function to call for this event. The mousedown does not always result in a drag operation. |
| onStartDrag | null | The JavaScript function to call for this event. Occurs after a mouse down and the drag threshold has been met. The drag threshold default is either 3 pixels, or 1 second of holding the mousedown. |
| onDrag | null | The JavaScript function to call for this event. Occurs every mousemove event while dragging. |
| onDragEnter | null | The JavaScript function to call for this event. Occurs when the dragged object first interacts with another targetable drag and drop object. |
| onDragOver | null | The JavaScript function to call for this event. Fires every mousemove event while over a drag and drop object. |
| onDragOut | null | The JavaScript function to call for this event. Fires when a dragged object is no longer over an object that had the onDragEnter fire. |
| onDragDrop | null | The JavaScript function to call for this event. Fires when the dragged objects is dropped on another. |
| endDrag | By default set to an internal method that prevents the endDrag from moving the original location of the dragged item. Currently assumes a proxy style drag. | The JavaScript function to call for this event. Fires on the mouseup event after a drag has been initiated (startDrag fired). |
| onMouseUp | null | The JavaScript function to call for this event. Fires on the mouseup event whether or not a drag was initiated. |
You can download the proto.js file here: