Draggable Widgetversion added: 1.0
Description: Allow elements to be moved using the mouse.
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:
-
ui-draggable
: The draggable element. When the draggable is disabled, theui-draggable-disabled
class is added. While dragging, theui-draggable-dragging
class is added. -
ui-draggable-handle
: The handle of the draggable, specified using thehandle
option. By default, the draggable element itself is also the handle.
Dependencies
Options
addClasses
true
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.Initialize the draggable with the addClasses
option specified:
1
2
3
|
|
Get or set the addClasses
option, after initialization:
1
2
3
4
5
|
|
appendTo
"parent"
Which element the draggable helper should be appended to while dragging.
appendTo
option only works when the helper
option is set to not use the original element.- jQuery: A jQuery object containing the element to append the helper to.
- Element: The element to append the helper to.
- Selector: A selector specifying which element to append the helper to.
-
String: The string
"parent"
will cause the helper to be a sibling of the draggable.
Initialize the draggable with the appendTo
option specified:
1
2
3
|
|
Get or set the appendTo
option, after initialization:
1
2
3
4
5
|
|
axis
false
"x"
, "y"
.Initialize the draggable with the axis
option specified:
1
2
3
|
|
Get or set the axis
option, after initialization:
1
2
3
4
5
|
|
cancel
"input,textarea,button,select,option"
Initialize the draggable with the cancel
option specified:
1
2
3
|
|
Get or set the cancel
option, after initialization:
1
2
3
4
5
|
|
classes
{}
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.
Initialize the draggable with the classes
option specified, changing the theming for the ui-draggable
class:
1
2
3
4
5
|
|
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
|
|
connectToSortable
false
helper
option must be set to "clone"
in order to work flawlessly. Requires the jQuery UI Sortable plugin to be included.Initialize the draggable with the connectToSortable
option specified:
1
2
3
|
|
Get or set the connectToSortable
option, after initialization:
1
2
3
4
5
|
|
containment
false
- Selector: The draggable element will be contained to the bounding box of the first element found by the selector. If no element is found, no containment will be set.
- Element: The draggable element will be contained to the bounding box of this element.
-
String: Possible values:
"parent"
,"document"
,"window"
. -
Array: An array defining a bounding box in the form
[ x1, y1, x2, y2 ]
.
Initialize the draggable with the containment
option specified:
1
2
3
|
|
Get or set the containment
option, after initialization:
1
2
3
4
5
|
|
cursor
"auto"
Initialize the draggable with the cursor
option specified:
1
2
3
|
|
Get or set the cursor
option, after initialization:
1
2
3
4
5
|
|
cursorAt
false
{ top, left, right, bottom }
.Initialize the draggable with the cursorAt
option specified:
1
2
3
|
|
Get or set the cursorAt
option, after initialization:
1
2
3
4
5
|
|
delay
0
Initialize the draggable with the delay
option specified:
1
2
3
|
|
Get or set the delay
option, after initialization:
1
2
3
4
5
|
|
disabled
false
true
.Initialize the draggable with the disabled
option specified:
1
2
3
|
|
Get or set the disabled
option, after initialization:
1
2
3
4
5
|
|
distance
1
Initialize the draggable with the distance
option specified:
1
2
3
|
|
Get or set the distance
option, after initialization:
1
2
3
4
5
|
|
grid
false
[ x, y ]
.Initialize the draggable with the grid
option specified:
1
2
3
|
|
Get or set the grid
option, after initialization:
1
2
3
4
5
|
|
handle
false
Initialize the draggable with the handle
option specified:
1
2
3
|
|
Get or set the handle
option, after initialization:
1
2
3
4
5
|
|
helper
"original"
-
String: If set to
"clone"
, then the element will be cloned and the clone will be dragged. - Function: A function that will return a DOMElement to use while dragging.
Initialize the draggable with the helper
option specified:
1
2
3
|
|
Get or set the helper
option, after initialization:
1
2
3
4
5
|
|
iframeFix
false
cursorAt
option, or in any case where the mouse cursor may not be over the helper.-
Boolean: When set to
true
, transparent overlays will be placed over all iframes on the page. - Selector: Any iframes matching the selector will be covered by transparent overlays.
Initialize the draggable with the iframeFix
option specified:
1
2
3
|
|
Get or set the iframeFix
option, after initialization:
1
2
3
4
5
|
|
opacity
false
Initialize the draggable with the opacity
option specified:
1
2
3
|
|
Get or set the opacity
option, after initialization:
1
2
3
4
5
|
|
refreshPositions
false
true
, all droppable positions are calculated on every mousemove.
Caution: This solves issues on highly dynamic pages, but dramatically decreases performance.
Initialize the draggable with the refreshPositions
option specified:
1
2
3
|
|
Get or set the refreshPositions
option, after initialization:
1
2
3
4
5
|
|
revert
false
-
Boolean: If set to
true
the element will always revert. -
String: If set to
"invalid"
, revert will only occur if the draggable has not been dropped on a droppable. For"valid"
, it's the other way around. -
Function: A function to determine whether the element should revert to its start position. The function must return
true
to revert the element.
Initialize the draggable with the revert
option specified:
1
2
3
|
|
Get or set the revert
option, after initialization:
1
2
3
4
5
|
|
revertDuration
500
revert
option is false
.Initialize the draggable with the revertDuration
option specified:
1
2
3
|
|
Get or set the revertDuration
option, after initialization:
1
2
3
4
5
|
|
scope
"default"
accept
option. A draggable with the same scope
value as a droppable will be accepted by the droppable.Initialize the draggable with the scope
option specified:
1
2
3
|
|
Get or set the scope
option, after initialization:
1
2
3
4
5
|
|
scroll
true
true
, container auto-scrolls while dragging.Initialize the draggable with the scroll
option specified:
1
2
3
|
|
Get or set the scroll
option, after initialization:
1
2
3
4
5
|
|
scrollSensitivity
20
scroll
option is false
.Initialize the draggable with the scrollSensitivity
option specified:
1
2
3
|
|
Get or set the scrollSensitivity
option, after initialization:
1
2
3
4
5
|
|
scrollSpeed
20
scrollSensitivity
distance. Ignored if the scroll
option is false
.Initialize the draggable with the scrollSpeed
option specified:
1
2
3
|
|
Get or set the scrollSpeed
option, after initialization:
1
2
3
4
5
|
|
snap
false
-
Boolean: When set to
true
, the element will snap to all other draggable elements. - Selector: A selector specifying which elements to snap to.
Initialize the draggable with the snap
option specified:
1
2
3
|
|
Get or set the snap
option, after initialization:
1
2
3
4
5
|
|
snapMode
"both"
snap
option is false
. Possible values: "inner"
, "outer"
, "both"
.Initialize the draggable with the snapMode
option specified:
1
2
3
|
|
Get or set the snapMode
option, after initialization:
1
2
3
4
5
|
|
snapTolerance
20
snap
option is false
.Initialize the draggable with the snapTolerance
option specified:
1
2
3
|
|
Get or set the snapTolerance
option, after initialization:
1
2
3
4
5
|
|
stack
false
Initialize the draggable with the stack
option specified:
1
2
3
|
|
Get or set the stack
option, after initialization:
1
2
3
4
5
|
|
zIndex
false
Initialize the draggable with the zIndex
option specified:
1
2
3
|
|
Get or set the zIndex
option, after initialization:
1
2
3
4
5
|
|
Methods
destroy()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the destroy method:
1
|
|
disable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the disable method:
1
|
|
enable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the enable method:
1
|
|
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.
- This method does not accept any arguments.
Invoke the instance method:
1
|
|
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.
-
optionNameType: StringThe name of the option to get.
Invoke the method:
1
|
|
option()Returns: PlainObject
- This signature does not accept any arguments.
Invoke the method:
1
|
|
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.
-
optionNameType: StringThe name of the option to set.
-
valueType: ObjectA value to set for the option.
Invoke the method:
1
|
|
option( options )Returns: jQuery (plugin only)
-
optionsType: ObjectA map of option-value pairs to set.
Invoke the method:
1
|
|
widget()Returns: jQuery
jQuery
object containing the draggable element.
- This method does not accept any arguments.
Invoke the widget method:
1
|
|
Events
create( event, ui )Type: dragcreate
Note: The ui
object is empty but included for consistency with other events.
Initialize the draggable with the create callback specified:
1
2
3
|
|
Bind an event listener to the dragcreate event:
1
|
|
drag( event, ui )Type: drag
-
eventType: Event
-
uiType: Object
-
helperType: jQueryThe jQuery object representing the helper that's being dragged.
-
positionType: ObjectCurrent CSS position of the helper as
{ top, left }
object. The values may be changed to modify where the element will be positioned. This is useful for custom containment, snapping, etc. -
offsetType: ObjectCurrent offset position of the helper as
{ top, left }
object.
-
Initialize the draggable with the drag callback specified:
1
2
3
|
|
Bind an event listener to the drag event:
1
|
|
Constrain movement via ui.position
:
1
2
3
4
5
6
7
8
|
|
start( event, ui )Type: dragstart
Initialize the draggable with the start callback specified:
1
2
3
|
|
Bind an event listener to the dragstart event:
1
|
|
stop( event, ui )Type: dragstop
Initialize the draggable with the stop callback specified:
1
2
3
|
|
Bind an event listener to the dragstop event:
1
|
|
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
|
|