MDN may have intermittent access issues April 18 13:00 - April 19 01:00 UTC. See whistlepig.mozilla.org for all notifications.

mozilla
Your Search Results

    Drag and drop

    Firefox and other Mozilla applications support a number of features for handling drag and drop. This allows the user to click and hold the mouse button down over an element, drag it to another location, and release the mouse button to drop the element there. A translucent representation of what is being dragged will follow the mouse pointer during the drag operation. The drop location may be a different application. Web sites, extensions, and XUL applications may make use of this functionality to customize what elements may be dragged and the drag feedback, as well as specify where elements may be dropped.

    This section covers drag and drop in Firefox 3.5 (Gecko 1.9.1) and later. For the older API for Firefox 3.0 and earlier, which is not exposed to web pages, see the older API documentation.

    Drag and Drop Basics

    To drag and drop, generally there are few steps to follow:

    • Define a draggable target. Specify the draggable attribute as true for the element we would like to drag. For information about this, see The Draggable Attribute.
    • Define the data to be dragged, which may be of a number of different formats. For example, textual data containing a string of text being dragged. For information about this, see Drag Data.
    • The drag feedback image which appears beside the mouse pointer during the drag operation. This image might be customized some of the time. Most of the time it won't be and a default image will be generated based on the element where the mouse was pressed down. To learn more about drag feedback images, see Setting the Drag Feedback Image.
    • The Drag effects that are allowed. Three such effects are possible: copy to indicate that the data being dragged will be copied from its present location to the drop location, move to indicate that the data being dragged will be moved, and link to indicate that some form of relationship or connection will be created between the source and drop locations. During the drag operation, the drag effects may be modified to indicate that certain effects are allowed at certain locations. If allowed, a drop may occur at that location. See Drag Effects for more details.
    • Define a drop area. By default, the browser prevents anything from happening when dropping something onto the HTML element. So to make an element a drop area, just stop the browser from doing the default action. That is, register the on "ondragenter" or "ondragover" event. For more infomation, please see Specifying Drop Targets.
    • Do something when the drop occurs. You may want to retrieve the data carried by the draggable target and do something accordingly. For more infomation, please see Performing a Drop.

    Mozilla and Firefox support a number of features not in the standard drag and drop model. These allow the user to drag multiple items and drag non-string data. For more information, see Dragging and Dropping Multiple Items.

    For a list of common data types used for drag and drop, see Recommended Drag Types.

    A quick reference is available for the best practice for dragging the following kinds of items:

    See DataTransfer for a reference to the DataTransfer object.

    Drag Events

    A number of events are used which are fired during various stages of the drag and drop operation. Note that only drag events are fired during a drag operation; mouse events such as mousemove are not. Also note that dragstart and dragend are not fired when dragging a file into the browser from the OS.

    The dataTransfer property of all drag events holds data about the drag and drop operation.

    dragstart
    Fired on an element when a drag is started. The user is requesting to drag the element where the dragstart event is fired. During this event, a listener would set information such as the drag data and image to be associated with the drag. This event is not fired when dragging a file into the browser from the OS. For information about this, see Starting a Drag Operation.
    dragenter
    Fired when the mouse enters an element while a drag is occurring. A listener for this event should indicate whether a drop is allowed over this location. If there are no listeners, or the listeners perform no operations, then a drop is not allowed by default. This is also the event to listen for in order to provide feedback that a drop is allowed, such as displaying a highlight or insertion marker. For information about this, see Specifying Drop Targets.
    dragover
    This event is fired as the mouse is moving over an element when a drag is occurring. Much of the time, the operation that occurs during a listener will be the same as the dragenter event. For information about this, see Specifying Drop Targets.
    dragleave
    This event is fired when the mouse leaves an element while a drag is occurring. Listeners should remove any highlighting or insertion markers used for drop feedback.
    drag
    This event is fired at the source of the drag and is the element where dragstart was fired during the drag operation.
    drop
    The drop event is fired on the element where the drop occurred at the end of the drag operation. A listener would be responsible for retrieving the data being dragged and inserting it at the drop location. This event will only fire if a drop is desired. It will not fire if the user cancelled the drag operation, for example by pressing the Escape key, or if the mouse button was released while the mouse was not over a valid drop target. For information about this, see Performing a Drop.
    dragend
    The source of the drag will receive a dragend event when the drag operation is complete, whether it was successful or not. This event is not fired when dragging a file into the browser from the OS. For more information about this, see Finishing a Drag.

    See also

    Document Tags and Contributors

    Last updated by: yihangho,