Draggable Widget | jQuery UI API Documentation (original) (raw)

Description: Allow elements to be moved using the mouse.

QuickNavExamples

Options

Methods

Events

Make the selected elements draggable by mouse. If you want not just drag, but drag & drop, see the jQuery UI Droppable plugin, which provides a drop target for draggables.

Theming

The draggable widget uses the jQuery UI CSS framework to style its look and feel. If draggable specific styling is needed, the following CSS class names can be used for overrides or as keys for the classes option:

Dependencies

Options

addClasses

Default: true

If set to false, will prevent the ui-draggable class from being added. This may be desired as a performance optimization when calling .draggable() on hundreds of elements.

Code examples:

Initialize the draggable with the addClasses option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the addClasses option, after initialization:

1 2 3 4 5 var addClasses = (".selector").draggable("option","addClasses");( ".selector" ).draggable( "option", "addClasses" );(".selector").draggable("option","addClasses");( ".selector" ).draggable( "option", "addClasses", false );

appendTo

Default: "parent"

Which element the draggable helper should be appended to while dragging.

Note: The appendTo option only works when the helper option is set to not use the original element.

Multiple types supported:

Code examples:

Initialize the draggable with the appendTo option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the appendTo option, after initialization:

1 2 3 4 5 var appendTo = (".selector").draggable("option","appendTo");( ".selector" ).draggable( "option", "appendTo" );(".selector").draggable("option","appendTo");( ".selector" ).draggable( "option", "appendTo", "body" );

axis

Default: false

Constrains dragging to either the horizontal (x) or vertical (y) axis. Possible values: "x", "y".

Code examples:

Initialize the draggable with the axis option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the axis option, after initialization:

1 2 3 4 5 var axis = (".selector").draggable("option","axis");( ".selector" ).draggable( "option", "axis" );(".selector").draggable("option","axis");( ".selector" ).draggable( "option", "axis", "x" );

cancel

Default: "input,textarea,button,select,option"

Prevents dragging from starting on specified elements.

Code examples:

Initialize the draggable with the cancel option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the cancel option, after initialization:

1 2 3 4 5 var cancel = (".selector").draggable("option","cancel");( ".selector" ).draggable( "option", "cancel" );(".selector").draggable("option","cancel");( ".selector" ).draggable( "option", "cancel", ".title" );

classes

Default: {}

Specify additional classes to add to the widget's elements. Any of classes specified in the Theming section can be used as keys to override their value. To learn more about this option, check out the learn article about the classes option.

Code examples:

Initialize the draggable with the classes option specified, changing the theming for the ui-draggable class:

1 2 3 4 5 $( ".selector" ).draggable({ "ui-draggable": "highlight"

Get or set a property of the classes option, after initialization, here reading and changing the theming for the ui-draggable class:

1 2 3 4 5 var themeClass = (".selector").draggable("option","classes.ui−draggable");( ".selector" ).draggable( "option", "classes.ui-draggable" );(".selector").draggable("option","classes.uidraggable");( ".selector" ).draggable( "option", "classes.ui-draggable", "highlight" );

connectToSortable

Default: false

Allows the draggable to be dropped onto the specified sortables. If this option is used, a draggable can be dropped onto a sortable list and then becomes part of it. Note: The helper option must be set to "clone" in order to work flawlessly. Requires the jQuery UI Sortable plugin to be included.

Code examples:

Initialize the draggable with the connectToSortable option specified:

1 2 3 $( ".selector" ).draggable({ connectToSortable: "#my-sortable"

Get or set the connectToSortable option, after initialization:

1 2 3 4 5 var connectToSortable = (".selector").draggable("option","connectToSortable");( ".selector" ).draggable( "option", "connectToSortable" );(".selector").draggable("option","connectToSortable");( ".selector" ).draggable( "option", "connectToSortable", "#my-sortable" );

containment

Default: false

Constrains dragging to within the bounds of the specified element or region.

Multiple types supported:

Code examples:

Initialize the draggable with the containment option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the containment option, after initialization:

1 2 3 4 5 var containment = (".selector").draggable("option","containment");( ".selector" ).draggable( "option", "containment" );(".selector").draggable("option","containment");( ".selector" ).draggable( "option", "containment", "parent" );

cursor

Default: "auto"

The CSS cursor during the drag operation.

Code examples:

Initialize the draggable with the cursor option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the cursor option, after initialization:

1 2 3 4 5 var cursor = (".selector").draggable("option","cursor");( ".selector" ).draggable( "option", "cursor" );(".selector").draggable("option","cursor");( ".selector" ).draggable( "option", "cursor", "crosshair" );

cursorAt

Default: false

Sets the offset of the dragging helper relative to the mouse cursor. Coordinates can be given as a hash using a combination of one or two keys: { top, left, right, bottom }.

Code examples:

Initialize the draggable with the cursorAt option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the cursorAt option, after initialization:

1 2 3 4 5 var cursorAt = (".selector").draggable("option","cursorAt");( ".selector" ).draggable( "option", "cursorAt" );(".selector").draggable("option","cursorAt");( ".selector" ).draggable( "option", "cursorAt", { left: 5 } );

delay

Default: 0

Time in milliseconds after mousedown until dragging should start. This option can be used to prevent unwanted drags when clicking on an element. (version deprecated: 1.12)

Code examples:

Initialize the draggable with the delay option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the delay option, after initialization:

1 2 3 4 5 var delay = (".selector").draggable("option","delay");( ".selector" ).draggable( "option", "delay" );(".selector").draggable("option","delay");( ".selector" ).draggable( "option", "delay", 300 );

disabled

Default: false

Disables the draggable if set to true.

Code examples:

Initialize the draggable with the disabled option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the disabled option, after initialization:

1 2 3 4 5 var disabled = (".selector").draggable("option","disabled");( ".selector" ).draggable( "option", "disabled" );(".selector").draggable("option","disabled");( ".selector" ).draggable( "option", "disabled", true );

distance

Default: 1

Distance in pixels after mousedown the mouse must move before dragging should start. This option can be used to prevent unwanted drags when clicking on an element. (version deprecated: 1.12)

Code examples:

Initialize the draggable with the distance option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the distance option, after initialization:

1 2 3 4 5 var distance = (".selector").draggable("option","distance");( ".selector" ).draggable( "option", "distance" );(".selector").draggable("option","distance");( ".selector" ).draggable( "option", "distance", 10 );

grid

Default: false

Snaps the dragging helper to a grid, every x and y pixels. The array must be of the form [ x, y ].

Code examples:

Initialize the draggable with the grid option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the grid option, after initialization:

1 2 3 4 5 var grid = (".selector").draggable("option","grid");( ".selector" ).draggable( "option", "grid" );(".selector").draggable("option","grid");( ".selector" ).draggable( "option", "grid", [ 50, 20 ] );

handle

Default: false

If specified, restricts dragging from starting unless the mousedown occurs on the specified element(s). Only elements that descend from the draggable element are permitted.

Code examples:

Initialize the draggable with the handle option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the handle option, after initialization:

1 2 3 4 5 var handle = (".selector").draggable("option","handle");( ".selector" ).draggable( "option", "handle" );(".selector").draggable("option","handle");( ".selector" ).draggable( "option", "handle", "h2" );

helper

Default: "original"

Allows for a helper element to be used for dragging display.

Multiple types supported:

Code examples:

Initialize the draggable with the helper option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the helper option, after initialization:

1 2 3 4 5 var helper = (".selector").draggable("option","helper");( ".selector" ).draggable( "option", "helper" );(".selector").draggable("option","helper");( ".selector" ).draggable( "option", "helper", "clone" );

iframeFix

Default: false

Prevent iframes from capturing the mousemove events during a drag. Useful in combination with the cursorAt option, or in any case where the mouse cursor may not be over the helper.

Multiple types supported:

Code examples:

Initialize the draggable with the iframeFix option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the iframeFix option, after initialization:

1 2 3 4 5 var iframeFix = (".selector").draggable("option","iframeFix");( ".selector" ).draggable( "option", "iframeFix" );(".selector").draggable("option","iframeFix");( ".selector" ).draggable( "option", "iframeFix", true );

opacity

Default: false

Opacity for the helper while being dragged.

Code examples:

Initialize the draggable with the opacity option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the opacity option, after initialization:

1 2 3 4 5 var opacity = (".selector").draggable("option","opacity");( ".selector" ).draggable( "option", "opacity" );(".selector").draggable("option","opacity");( ".selector" ).draggable( "option", "opacity", 0.35 );

refreshPositions

Default: false

If set to true, all droppable positions are calculated on every mousemove.Caution: This solves issues on highly dynamic pages, but dramatically decreases performance.

Code examples:

Initialize the draggable with the refreshPositions option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the refreshPositions option, after initialization:

1 2 3 4 5 var refreshPositions = (".selector").draggable("option","refreshPositions");( ".selector" ).draggable( "option", "refreshPositions" );(".selector").draggable("option","refreshPositions");( ".selector" ).draggable( "option", "refreshPositions", true );

revert

Default: false

Whether the element should revert to its start position when dragging stops.

Multiple types supported:

Code examples:

Initialize the draggable with the revert option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the revert option, after initialization:

1 2 3 4 5 var revert = (".selector").draggable("option","revert");( ".selector" ).draggable( "option", "revert" );(".selector").draggable("option","revert");( ".selector" ).draggable( "option", "revert", true );

revertDuration

Default: 500

The duration of the revert animation, in milliseconds. Ignored if the revert option is false.

Code examples:

Initialize the draggable with the revertDuration option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the revertDuration option, after initialization:

1 2 3 4 5 var revertDuration = (".selector").draggable("option","revertDuration");( ".selector" ).draggable( "option", "revertDuration" );(".selector").draggable("option","revertDuration");( ".selector" ).draggable( "option", "revertDuration", 200 );

scope

Default: "default"

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.

Code examples:

Initialize the draggable with the scope option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the scope option, after initialization:

1 2 3 4 5 var scope = (".selector").draggable("option","scope");( ".selector" ).draggable( "option", "scope" );(".selector").draggable("option","scope");( ".selector" ).draggable( "option", "scope", "tasks" );

snap

Default: false

Whether the element should snap to other elements.

Multiple types supported:

Code examples:

Initialize the draggable with the snap option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the snap option, after initialization:

1 2 3 4 5 var snap = (".selector").draggable("option","snap");( ".selector" ).draggable( "option", "snap" );(".selector").draggable("option","snap");( ".selector" ).draggable( "option", "snap", true );

snapMode

Default: "both"

Determines which edges of snap elements the draggable will snap to. Ignored if the snap option is false. Possible values: "inner", "outer", "both".

Code examples:

Initialize the draggable with the snapMode option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the snapMode option, after initialization:

1 2 3 4 5 var snapMode = (".selector").draggable("option","snapMode");( ".selector" ).draggable( "option", "snapMode" );(".selector").draggable("option","snapMode");( ".selector" ).draggable( "option", "snapMode", "inner" );

snapTolerance

Default: 20

The distance in pixels from the snap element edges at which snapping should occur. Ignored if the snap option is false.

Code examples:

Initialize the draggable with the snapTolerance option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the snapTolerance option, after initialization:

1 2 3 4 5 var snapTolerance = (".selector").draggable("option","snapTolerance");( ".selector" ).draggable( "option", "snapTolerance" );(".selector").draggable("option","snapTolerance");( ".selector" ).draggable( "option", "snapTolerance", 30 );

stack

Default: false

Controls the z-index of the set of elements that match the selector, always brings the currently dragged item to the front. Very useful in things like window managers.

Code examples:

Initialize the draggable with the stack option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the stack option, after initialization:

1 2 3 4 5 var stack = (".selector").draggable("option","stack");( ".selector" ).draggable( "option", "stack" );(".selector").draggable("option","stack");( ".selector" ).draggable( "option", "stack", ".products" );

zIndex

Default: false

Z-index for the helper while being dragged.

Code examples:

Initialize the draggable with the zIndex option specified:

1 2 3 $( ".selector" ).draggable({

Get or set the zIndex option, after initialization:

1 2 3 4 5 var zIndex = (".selector").draggable("option","zIndex");( ".selector" ).draggable( "option", "zIndex" );(".selector").draggable("option","zIndex");( ".selector" ).draggable( "option", "zIndex", 100 );

Methods

destroy()Returns: jQuery (plugin only)

Removes the draggable functionality completely. This will return the element back to its pre-init state.

Code examples:

Invoke the destroy method:

1 $( ".selector" ).draggable( "destroy" );

disable()Returns: jQuery (plugin only)

Disables the draggable.

Code examples:

Invoke the disable method:

1 $( ".selector" ).draggable( "disable" );

enable()Returns: jQuery (plugin only)

Enables the draggable.

Code examples:

Invoke the enable method:

1 $( ".selector" ).draggable( "enable" );

instance()Returns: Object

Retrieves the draggable's instance object. If the element does not have an associated instance, undefined is returned.

Unlike other widget methods, instance() is safe to call on any element after the draggable plugin has loaded.

Code examples:

Invoke the instance method:

1 $( ".selector" ).draggable( "instance" );

option( optionName )Returns: Object

Gets the value currently associated with the specified optionName.

Note: For options that have objects as their value, you can get the value of a specific key by using dot notation. For example, "foo.bar" would get the value of the bar property on the foo option.

Code examples:

Invoke the method:

1 var isDisabled = $( ".selector" ).draggable( "option", "disabled" );

option()Returns: PlainObject

Gets an object containing key/value pairs representing the current draggable options hash.

Code examples:

Invoke the method:

1 var options = $( ".selector" ).draggable( "option" );

option( optionName, value )Returns: jQuery (plugin only)

Sets the value of the draggable option associated with the specified optionName.

Note: For options that have objects as their value, you can set the value of just one property by using dot notation for optionName. For example, "foo.bar" would update only the bar property of the foo option.

Code examples:

Invoke the method:

1 $( ".selector" ).draggable( "option", "disabled", true );

option( options )Returns: jQuery (plugin only)

Sets one or more options for the draggable.

Code examples:

Invoke the method:

1 $( ".selector" ).draggable( "option", { disabled: true } );

Events

create( event, ui )Type: dragcreate

Triggered when the draggable is created.

Note: The ui object is empty but included for consistency with other events.

Code examples:

Initialize the draggable with the create callback specified:

1 2 3 $( ".selector" ).draggable({ create: function( event, ui ) {}

Bind an event listener to the dragcreate event:

1 $( ".selector" ).on( "dragcreate", function( event, ui ) {} );

drag( event, ui )Type: drag

Triggered while the mouse is moved during the dragging, immediately before the current move happens.

Code examples:

Initialize the draggable with the drag callback specified:

1 2 3 $( ".selector" ).draggable({ drag: function( event, ui ) {}

Bind an event listener to the drag event:

1 $( ".selector" ).on( "drag", function( event, ui ) {} );

Constrain movement via ui.position:

1 2 3 4 5 6 7 8 $( ".selector" ).draggable({ drag: function( event, ui ) { // Keep the left edge of the element // at least 100 pixels from the container ui.position.left = Math.min( 100, ui.position.left );

start( event, ui )Type: dragstart

Triggered when dragging starts.

Code examples:

Initialize the draggable with the start callback specified:

1 2 3 $( ".selector" ).draggable({ start: function( event, ui ) {}

Bind an event listener to the dragstart event:

1 $( ".selector" ).on( "dragstart", function( event, ui ) {} );

stop( event, ui )Type: dragstop

Triggered when dragging stops.

Code examples:

Initialize the draggable with the stop callback specified:

1 2 3 $( ".selector" ).draggable({ stop: function( event, ui ) {}

Bind an event listener to the dragstop event:

1 $( ".selector" ).on( "dragstop", function( event, ui ) {} );

Example:

A simple jQuery UI Draggable

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <title>draggable demo</title> <link rel="stylesheet" href="" title="undefined" rel="noopener noreferrer">https://code.jquery.com/ui/1.13.3/themes/smoothness/jquery-ui.css"\> <script src="" title="undefined" rel="noopener noreferrer">https://code.jquery.com/jquery-3.7.1.js"\></script> <script src="" title="undefined" rel="noopener noreferrer">https://code.jquery.com/ui/1.13.3/jquery-ui.js"\></script><div id="draggable">Drag me</div>$( "#draggable" ).draggable();

Demo: