Easy Tutorial
❮ Jqueryui Theme Jqueryui Widget Extend ❯

jQuery UI API - Draggable Widget

Category

Interactions

Usage

Description: Allows elements to be moved using the mouse.

Version Added: 1.0

Dependencies:

Note: Makes the selected element draggable with the mouse. If you want to drag and drop, see the jQuery UI Droppable plugin which provides a drop target for draggable elements.

Quick Navigation

Options Methods Events
addClasses appendTo axis cancel connectToSortable containment cursor cursorAt delay disabled distance grid handle helper iframeFix opacity refreshPositions revert revertDuration scope scroll scrollSensitivity scrollSpeed snap snapMode snapTolerance stack zIndex destroy disable enable option widget create drag start stop
Option Type Description Default Value
addClasses Boolean If set to false, prevents the ui-draggable class from being added. This is useful for performance optimization when calling .draggable() on hundreds of elements. <br> Code example: Initialize draggable with the specified addClasses option: $( ".selector" ).draggable({ addClasses: false }); After initialization, get or set the addClasses option: // getter<br>var addClasses = $( ".selector" ).draggable( "option", "addClasses" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "addClasses", false ); true
appendTo jQuery or Element or Selector or String Which element the draggable helper should be appended to while dragging. <br> Supports various types: jQuery: A jQuery object containing the element to append the helper to. <br> Element: The element to append the helper to. <br> Selector: A selector specifying which element to append the helper to. <br> String: The string "parent" will cause the helper to become a sibling of the draggable. <br> Code example: Initialize draggable with the specified appendTo option: $( ".selector" ).draggable({ appendTo: "body" }); After initialization, get or set the appendTo option: // getter<br>var appendTo = $( ".selector" ).draggable( "option", "appendTo" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "appendTo", "body" ); "parent"
axis String Constrains dragging to either the horizontal (x) or vertical (y) axis. Possible values: "x", "y". <br> Code example: Initialize draggable with specified axis option: $( ".selector" ).draggable({ axis: "x" }); After initialization, get or set the axis option: // getter<br>var axis = $( ".selector" ).draggable( "option", "axis" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "axis", "x" ); false
cancel Selector Prevents dragging from starting on specified elements. <br> Code example: Initialize draggable with specified cancel option: $( ".selector" ).draggable({ cancel: ".title" }); After initialization, get or set the cancel option: // getter<br>var cancel = $( ".selector" ).draggable( "option", "cancel" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "cancel", ".title" ); "input, textarea, button, select, option"
connectToSortable Selector Allows the draggable to be dropped onto the specified sortable. If used, a draggable can be dropped onto a sortable list and become part of it. Note: The helper option must be set to "clone" for this to work properly. Requires the Sortable Widget <br> Code example: Initialize draggable with specified connectToSortable option: $( ".selector" ).draggable({ connectToSortable: "#my-sortable" }); After initialization, get or set the connectToSortable option: // getter<br>var connectToSortable = $( ".selector" ).draggable( "option", "connectToSortable" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "connectToSortable", "#my-sortable" ); false
containment Selector or Element or String or Array Constrains dragging to within the bounds of the specified element or region. <br> Supports multiple types: Selector: The draggable element will be contained within the bounds of the first element found by the selector. If no element is found, containment is not set.<br> Element: The draggable element will be contained within the bounds of the element.<br> String: Possible values: "parent", "document", "window".<br> Array: An array defining the boundaries in the form [ x1, y1, x2, y2 ]. Code example: Initialize draggable with specified containment option: $( ".selector" ).draggable({ containment: "parent" }); After initialization, get or set the containment option: // getter<br>var containment = $( ".selector" ).draggable( "option", "containment" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "containment", "parent" ); false
cursor String CSS cursor during drag operation. <br> Code example: Initialize draggable with specified cursor option: $( ".selector" ).draggable({ cursor: "crosshair" }); After initialization, get or set the cursor option: // getter<br>var cursor = $( ".selector" ).draggable( "option", "cursor" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "cursor", "crosshair" ); "auto"
cursorAt Object Sets the offset of the dragging helper relative to the mouse cursor. Coordinates can be given as a combination of one or two keys in a hash: { top, left, right, bottom }. <br> Code example: Initialize draggable with specified cursorAt option: $( ".selector" ).draggable({ cursorAt: { left: 5 } }); After initialization, get or set the cursorAt option: // getter<br>var cursorAt = $( ".selector" ).draggable( "option", "cursorAt" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "cursorAt", { left: 5 } ); false
delay Number Time in milliseconds after mousedown until dragging should start. This option can prevent unwanted drags when clicking on an element. <br> Code example: Initialize draggable with specified delay option: $( ".selector" ).draggable({ delay: 300 }); After initialization, get or set the delay option: // getter<br>var delay = $( ".selector" ).draggable( "option", "delay" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "delay", 300 ); 0
disabled Boolean If set to true, disables the draggable. <br> Code example: Initialize draggable with specified disabled option: $( ".selector" ).draggable({ disabled: true }); After initialization, get or set the disabled option: // getter<br>var disabled = $( ".selector" ).draggable( "option", "disabled" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "disabled", true ); false
distance Number Distance in pixels after mousedown the mouse must move before dragging starts. This option can prevent unwanted drags when clicking on an element. <br> Code example: Initialize draggable with specified distance option: $( ".selector" ).draggable({ distance: 10 }); After initialization, get or set the distance option: // getter<br>var distance = $( ".selector" ).draggable( "option", "distance" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "distance", 10 ); 1
grid Array Align the draggable helper to the grid, with each x and y pixel. The array format must be [x, y]. <br> Code example: Initialize draggable with specified grid option: $( ".selector" ).draggable({ grid: [ 50, 20 ] }); After initialization, get or set the grid option: // getter<br>var grid = $( ".selector" ).draggable( "option", "grid" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "grid", [ 50, 20 ] ); false
handle Selector or Element If specified, restricts starting the drag operation unless the mouse is pressed on the specified element. Only descendants of the draggable element are allowed to be dragged. <br> Code example: Initialize draggable with specified handle option: $( ".selector" ).draggable({ handle: "h2" }); After initialization, get or set the handle option: // getter<br>var handle = $( ".selector" ).draggable( "option", "handle" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "handle", "h2" ); false
helper String or Function() Allows for a helper element to be used for dragging display. <br> Supports multiple types: String: If set to "clone", the element will be cloned and the clone will be dragged.<br> Function: A function that returns a DOMElement to use for dragging. Code example: Initialize draggable with specified helper option: $( ".selector" ).draggable({ helper: "clone" }); After initialization, get or set the helper option: // getter<br>var helper = $( ".selector" ).draggable( "option", "helper" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "helper", "clone" ); "original"
iframeFix Boolean or Selector Prevents iframes from capturing the mousemove events during dragging. Useful in combination with the cursorAt option, or when the mouse cursor is not over the helper. <br> Supports multiple types: Boolean: When set to true, a transparent overlay will be placed over all iframes on the page.<br> Selector: Any iframes matching the selector will be covered with a transparent overlay. Code example: Initialize draggable with specified iframeFix option: $( ".selector" ).draggable({ iframeFix: true }); After initialization, get or set the iframeFix option: // getter<br>var iframeFix = $( ".selector" ).draggable( "option", "iframeFix" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "iframeFix", true ); false
opacity Number The opacity of the helper while being dragged. <br> Code example: Initialize draggable with specified opacity option: $( ".selector" ).draggable({ opacity: 0.35 }); After initialization, get or set the opacity option: // getter<br>var opacity = $( ".selector" ).draggable( "option", "opacity" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "opacity", 0.35 ); false
refreshPositions Boolean If set to true, all droppable positions are recalculated on every mousemove event. Note: This solves highly dynamic issues but significantly reduces performance. <br> Code example: Initialize draggable with the specified refreshPositions option: $( ".selector" ).draggable({ refreshPositions: true }); After initialization, get or set the refreshPositions option: // getter<br>var refreshPositions = $( ".selector" ).draggable( "option", "refreshPositions" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "refreshPositions", true ); false
revert Boolean or String or Function() Whether the element should revert to its start position when dragging stops. <br> Supports multiple types: Boolean: If set to true, the element always reverts. <br> String: If set to "invalid", reverts only if the draggable is not dropped on a droppable, and if set to "valid", it does the opposite. <br> Function: A function that determines whether the element should revert to its start position. The function must return true to revert the element. Code example: Initialize draggable with the specified revert option: $( ".selector" ).draggable({ revert: true }); After initialization, get or set the revert option: // getter<br>var revert = $( ".selector" ).draggable( "option", "revert" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "revert", true ); false
revertDuration Number The duration of the revert animation in milliseconds. Ignored if the revert option is false. <br> Code example: Initialize draggable with the specified revertDuration option: $( ".selector" ).draggable({ revertDuration: 200 }); After initialization, get or set the revertDuration option: // getter<br>var revertDuration = $( ".selector" ).draggable( "option", "revertDuration" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "revertDuration", 200 ); 500
scope String Used to group sets of draggable and droppable items, in addition to droppable's accept option. A draggable with the same scope value as a droppable will be accepted by the droppable. <br> Code example: Initialize draggable with the specified scope option: $( ".selector" ).draggable({ scope: "tasks" }); After initialization, get or set the scope option: // getter<br>var scope = $( ".selector" ).draggable( "option", "scope" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "scope", "tasks" ); "default"
scroll Boolean If set to true, the container will automatically scroll when dragging.

Code example: Initialize draggable with specified scroll option:
$(".selector").draggable({ scroll: false });
After initialization, get or set the scroll option:
// getter
var scroll = $(".selector").draggable("option", "scroll");
// setter
$(".selector").draggable("option", "scroll", false); | true | | scrollSensitivity | Number | The distance in pixels from the viewport edge to start scrolling. The distance is relative to the pointer, not the draggable. Ignored if the scroll option is false.
Code example: Initialize draggable with specified scrollSensitivity option:
$(".selector").draggable({ scrollSensitivity: 100 });
After initialization, get or set the scrollSensitivity option:
// getter
var scrollSensitivity = $(".selector").draggable("option", "scrollSensitivity");
// setter
$(".selector").draggable("option", "scrollSensitivity", 100); | 20 | | scrollSpeed | Number | The speed at which the window scrolls when the mouse pointer is within the scrollSensitivity distance. Ignored if the scroll option is false.
Code example: Initialize draggable with specified scrollSpeed option:
$(".selector").draggable({ scrollSpeed: 100 });
After initialization, get or set the scrollSpeed option:
// getter
var scrollSpeed = $(".selector").draggable("option", "scrollSpeed");
// setter
$(".selector").draggable("option", "scrollSpeed", 100); | 20 | | snap | Boolean or Selector | Whether the element should snap to other elements.
Supports multiple types:
Boolean: When set to true, the element will snap to other draggable elements.
Selector: A selector specifying which elements to snap to.
Code example: Initialize draggable with specified snap option:
$(".selector").draggable({ snap: true });
After initialization, get or set the snap option:
// getter
var snap = $(".selector").draggable("option", "snap");
// setter
$(".selector").draggable("option", "snap", true); | false | | snapMode | String | Determines which edge of the snap element the draggable will align to. Ignored if the snap option is false. Possible values: "inner", "outer", "both".
Code example: Initialize draggable with specified snapMode option:
$(".selector").draggable({ snapMode: "inner" });
After initialization, get or set the snapMode option:
// getter
var snapMode = $(".selector").draggable("option", "snapMode");
// setter
$(".selector").draggable("option", "snapMode", "inner"); | "both" | | snapTolerance | Number | The distance from the edge of the alignment element at which alignment occurs, measured in pixels. Ignored if the snap option is false. <br> Code example: Initialize draggable with specified snapTolerance option: $( ".selector" ).draggable({ snapTolerance: 30 }); After initialization, get or set the snapTolerance option: // getter<br>var snapTolerance = $( ".selector" ).draggable( "option", "snapTolerance" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "snapTolerance", 30 ); | 20 | | stack | Selector | Controls the z-index of the set of elements that match the selector, always bringing the currently dragged item to the front. Useful in things like window managers. <br> Code example: Initialize draggable with specified stack option: $( ".selector" ).draggable({ stack: ".products" }); After initialization, get or set the stack option: // getter<br>var stack = $( ".selector" ).draggable( "option", "stack" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "stack", ".products" ); | false | | zIndex | Number | Z-index for the helper when dragged. <br> Code example: Initialize draggable with specified zIndex option: $( ".selector" ).draggable({ zIndex: 100 }); After initialization, get or set the zIndex option: // getter<br>var zIndex = $( ".selector" ).draggable( "option", "zIndex" );<br> <br>// setter<br>$( ".selector" ).draggable( "option", "zIndex", 100 ); | false |

Method Returns Description
destroy() jQuery (plugin only) Completely removes the draggable functionality. This will return the element to its pre-initialization state. <br> This method does not accept any arguments. Code example: Call the destroy method: $( ".selector" ).draggable( "destroy" );
disable() jQuery (plugin only) Disables the draggable. <br> This method does not accept any arguments. Code example: Call the disable method: $( ".selector" ).draggable( "disable" );
enable() jQuery (plugin only) Enables the draggable. <br> This method does not accept any arguments. Code example: Call the enable method: $( ".selector" ).draggable( "enable" );
option( optionName ) Object Gets the value currently associated with the specified optionName. <br> optionName<br> Type: String<br> Description: The name of the option to get. Code example: Call the method: var isDisabled = $( ".selector" ).draggable( "option", "disabled" );
option() PlainObject Gets an object containing key/value pairs representing the current draggable options hash. <br> This method does not accept any arguments. Code example: Call the method: var options = $( ".selector" ).draggable( "option" );
option( optionName, value ) jQuery (plugin only) Sets the value of the draggable option associated with the specified optionName. <br> optionName<br> Type: String<br> Description: The name of the option to set.<br> <br> value<br> Type: Object<br> Description: The value to set for the option. Code example: Call this method: $( ".selector" ).draggable( "option", "disabled", true );
option( options ) jQuery (plugin only) Sets one or more options for the draggable. <br> options<br> Type: Object<br> Description: A map of option-value pairs to set. Code example: Call this method: $( ".selector" ).draggable( "option", { disabled: true } );
widget() jQuery Returns a jQuery object containing the draggable element. <br> This method does not accept any parameters. Code example: Call the widget method: var widget = $( ".selector" ).draggable( "widget" );
Event Type Description
create( event, ui ) dragcreate Triggered when the draggable is created. <br> event<br> Type: Event<br> <br> ui<br> Type: Object Note: The ui object is empty, included here for consistency with other events. Code example: Initialize a draggable with the specified create callback: $( ".selector" ).draggable({<br> create: function( event, ui ) {}<br>}); Bind an event listener to the dragcreate event: $( ".selector" ).on( "dragcreate", function( event, ui ) {} );
drag( event, ui ) drag Triggered while dragging when the mouse moves. <br> event<br> Type: Event<br> <br> ui<br> Type: Object<br> <br> helper<br> Type: jQuery<br> Description: The jQuery object representing the helper being dragged.<br> <br> position<br> Type: Object<br> Description: The current CSS position of the helper, e.g., a { top, left } object.<br> <br> offset<br> Type: Object<br> Description: The current offset position of the helper, e.g., a { top, left } object. Code example: Initialize a draggable with the specified drag callback: $( ".selector" ).draggable({<br> drag: function( event, ui ) {}<br>}); Bind an event listener to the drag event: $( ".selector" ).on( "drag", function( event, ui ) {} );
| start( event, ui ) | dragstart | Triggered when dragging starts. <br> event<br> Type: Event<br> <br> ui<br> Type: Object<br> <br> helper<br> Type: jQuery<br> Description: The jQuery object representing the helper being dragged. <br> <br> position<br> Type: Object<br> Description: The current CSS position of the helper, such as { top, left } object. <br> <br> offset<br> Type: Object<br> Description: The current offset position of the helper, such as { top, left } object. Code example: Initialize draggable with the specified start callback: $( ".selector" ).draggable({<br> start: function( event, ui ) {}<br>}); Bind an event listener to the dragstart event: $( ".selector" ).on( "dragstart", function( event, ui ) {} ); |
| stop( event, ui ) | dragstop | Triggered when dragging stops. <br> event<br> Type: Event<br> <br> ui<br> Type: Object<br> <br> helper<br> Type: jQuery<br> Description: The jQuery object representing the helper being dragged. <br> <br> position<br> Type: Object<br> Description: The current CSS position of the helper, such as { top, left } object. <br> <br> offset<br> Type: Object<br> Description: The current offset position of the helper, such as { top, left } object. Code example: Initialize draggable with the specified stop callback: $( ".selector" ).draggable({<br> stop: function( event, ui ) {}<br>}); Bind an event listener to the dragstop event: $( ".selector" ).on( "dragstop", function( event, ui ) {} ); |

## Example

A simple jQuery UI Draggable Widget.

<html lang="en"> <head> <meta charset="utf-8"> <title>Draggable Widget Demo</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <style> #draggable { width: 100px; height: 100px; background: #ccc; } </style> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> </head> <body>

<div id="draggable">Drag me!</div>

<script> $( "#draggable" ).draggable(); </script>

</body> </html>


[View Demo](../try/tryit.php?filename=jqueryui-api-draggable)
❮ Jqueryui Theme Jqueryui Widget Extend ❯