jQuery UI API - Draggable Widget
Category
Usage
**Description:** Allow elements to be moved using the mouse.
**Version added:** 1.0
Dependencies:
**Note:** Make the selected elements draggable with the mouse. If you want more than dragging - dragging & dropping - see the jQuery UI Droppable plugin, which provides a drop target for draggable elements.
Quick navigation
| Option | Method | Events |
|---|---|---|
| Option | Type | Description | Default value |
|---|---|---|---|
| addClasses | Boolean | If set to false, which will prevent the ui-draggable class from being added. When calling .draggable() on hundreds of elements, this setting helps with performance. |
代码实例:
初始化带有指定 addClasses 选项的 draggable:
$( ".selector" ).draggable({ addClasses: false });
After initialization, get or set addClasses Option:
// getter var addClasses = $( ".selector" ).draggable( "option", "addClasses" );| true| onSelect | event | appendTo | jQuery or Element or Selector or String | Which element the draggable helper should be appended to during a drag.// setter $( ".selector" ).draggable( "option", "addClasses", false );
Supports multiple types:
- jQuery: A jQuery object containing the element to append the helper to.
- Element: The element to append the helper to.
- SelectorappendTo: a selector specifying which element the helper is appended to.
- String: String
"parent"This will cause the helper to become a sibling of the draggable.
Code example:
Initializes with the specified appendTo The draggable option:
$( ".selector" ).draggable({ appendTo: "body" });
After initialization, get or set appendTo Option:
// getter var appendTo = $( ".selector" ).draggable( "option", "appendTo" );| "parent"| onSelect | event | axis | String | Constrains dragging to the horizontal (x) axis or the vertical (y) axis. Possible values:// setter $( ".selector" ).draggable( "option", "appendTo", "body" );
"x", "y"。
Code example:
Initializes with the specified axis Options of draggable:
$( ".selector" ).draggable({ axis: "x" });
After initialization, get or set axis Option:
// getter var axis = $( ".selector" ).draggable( "option", "axis" );| false| onSelect | event | cancel | Selector | Prevents dragging from starting on specified elements.// setter $( ".selector" ).draggable( "option", "axis", "x" );
Code example:
Initializes with the specified cancel Options of draggable:
$( ".selector" ).draggable({ cancel: ".title" });
After initialization, get or set cancel Option:
// getter var cancel = $( ".selector" ).draggable( "option", "cancel" );| "input, textarea, button, select, option"| onSelect | event | connectToSortable | Selector | Allows a draggable to be dropped on the specified sortable. If this option is used, a draggable can be dropped on a sortable list and then become part of the list. Note:// setter $( ".selector" ).draggable( "option", "cancel", ".title" );
helper The option must be set to "clone", in order to work better. It must contain Sortable Widget。
Code example:
Initializes with the specified connectToSortable The draggable option:
$( ".selector" ).draggable({ connectToSortable: "#my-sortable" });
After initialization, get or set connectToSortable Option:
// getter var connectToSortable = $( ".selector" ).draggable( "option", "connectToSortable" );| false| onSelect | event | containment | Selector or Element or String or Array | Constrains dragging to within the bounds of the specified element or region.// setter $( ".selector" ).draggable( "option", "connectToSortable", "#my-sortable" );
Multiple types are supported:
- SelectorSelector: the draggable element will be contained within the bounds of the first element matching the selector. If no element is found, containment is not set.
- ElementElement: the draggable element will be contained within the bounds of the element.
- String: possible values:
"parent"、"document"、"window"。 - Array: an array, in the form
[ x1, y1, x2, y2 ]Define the boundaries of the element.
Code example:
Initializes with the specified containment The draggable option:
$( ".selector" ).draggable({ containment: "parent" });
After initialization, get or set containment Option:
// getter var containment = $( ".selector" ).draggable( "option", "containment" );| false| onSelect | event | cursor | String | The CSS cursor during the drag operation.// setter $( ".selector" ).draggable( "option", "containment", "parent" );
Code example:
Initializes with the specified cursor The draggable option:
$( ".selector" ).draggable({ cursor: "crosshair" });
After initialization, get or set cursor Option:
// getter var cursor = $( ".selector" ).draggable( "option", "cursor" );| "auto"| onSelect | event | cursorAt | Object | Sets the offset of the drag helper (helper) relative to the mouse cursor. Coordinates can be given as a hash with one or two keys:// setter $( ".selector" ).draggable( "option", "cursor", "crosshair" );
{ top, left, right, bottom }。
Code example:
Initializes with the specified cursorAt The draggable option:
$( ".selector" ).draggable({ cursorAt: { left: 5 } });
After initialization, get or set cursorAt Option:
// getter var cursorAt = $( ".selector" ).draggable( "option", "cursorAt" );| false| onSelect | event | delay | Number | The time in milliseconds after mousedown until dragging starts. This option can prevent unwanted drags when clicking on an element.// setter $( ".selector" ).draggable( "option", "cursorAt", { left: 5 } );
Code example:
Initializes with the specified delay The draggable option:
$( ".selector" ).draggable({ delay: 300 });
After initialization, get or set delay Option:
// getter var delay = $( ".selector" ).draggable( "option", "delay" );| 0| onSelect | event | disabled | Boolean | If set to// setter $( ".selector" ).draggable( "option", "delay", 300 );
true, then the draggable is disabled.
Code example:
Initializes with the specified disabled The draggable option:
$( ".selector" ).draggable({ disabled: true });
After initialization, get or set disabled Option:
// getter var disabled = $( ".selector" ).draggable( "option", "disabled" );| false| onSelect | event | distance | Number | The distance in pixels the mouse must move after mousedown before dragging starts. This option can prevent unwanted drags when clicking on an element.// setter $( ".selector" ).draggable( "option", "disabled", true );
Code example:
Initializes with the specified distance The draggable option:
$( ".selector" ).draggable({ distance: 10 });
After initialization, get or set distance Option:
// getter var distance = $( ".selector" ).draggable( "option", "distance" );| 1| onSelect | event | grid | Array | Align the draggable helper to the grid, every x and y pixels. The array form must be// setter $( ".selector" ).draggable( "option", "distance", 10 );
[ x, y ]。
Code example:
Initializes with the specified grid The draggable option:
$( ".selector" ).draggable({ grid: [ 50, 20 ] });
After initialization, get or set grid Option:
// getter var grid = $( ".selector" ).draggable( "option", "grid" );| false| onSelect | event | handle | Selector or Element | If specified, restricts drag start to clicks on the specified element(s). Only descendants of the draggable element may be dragged.// setter $( ".selector" ).draggable( "option", "grid", [ 50, 20 ] );
Code example:
Initializes with the specified handle The draggable option:
$( ".selector" ).draggable({ handle: "h2" });
After initialization, get or set handle Option:
// getter var handle = $( ".selector" ).draggable( "option", "handle" );| false| onSelect | event | helper | String or Function() | Allows a helper element to be used for dragging display.// setter $( ".selector" ).draggable( "option", "handle", "h2" );
Multiple types are supported:
- String: if set to
"clone", the element will be cloned, and the clone will be dragged. - Function: A function that returns the DOMElement to be used for dragging.
Code example:
Initializes with the specified helper The draggable option:
$( ".selector" ).draggable({ helper: "clone" });
After initialization, get or set helper Option:
// getter var helper = $( ".selector" ).draggable( "option", "helper" );| "original"| onSelect | event | iframeFix | Boolean or Selector | Prevents iframes from capturing mouse move (mousemove) events during dragging. When used with// setter $( ".selector" ).draggable( "option", "helper", "clone" );
cursorAt Very useful when used in combination with other options, or when the mouse cursor is not over the helper.
Multiple types are supported:
- Boolean: when set to
truewhen, a transparent overlay is placed over all iframes on the page. - Selector: Any iframes matching the selector will be covered by a transparent overlay.
Code example:
Initializes with the specified iframeFix The draggable option:
$( ".selector" ).draggable({ iframeFix: true });
After initialization, get or set iframeFix Option:
// getter var iframeFix = $( ".selector" ).draggable( "option", "iframeFix" );| false| onSelect | event | opacity | Number | The opacity of the helper while being dragged.// setter $( ".selector" ).draggable( "option", "iframeFix", true );
Code example:
Initializes with the specified opacity The draggable option:
$( ".selector" ).draggable({ opacity: 0.35 });
After initialization, get or set opacity Option:
// getter var opacity = $( ".selector" ).draggable( "option", "opacity" );| false| onSelect | event | refreshPositions | Boolean | If set to// setter $( ".selector" ).draggable( "option", "opacity", 0.35 );
true, all droppable positions are calculated on every mouse move (mousemove). Note: This solves the problem of high dynamism, but significantly reduces performance.
Code example:
Initializes with the specified refreshPositions The draggable option:
$( ".selector" ).draggable({ refreshPositions: true });
After initialization, get or set refreshPositions Option:
// getter var refreshPositions = $( ".selector" ).draggable( "option", "refreshPositions" );| false| onSelect | event | revert | Boolean or String or Function() | Whether the element reverts to its starting position when dragging stops.// setter $( ".selector" ).draggable( "option", "refreshPositions", true );
Supports multiple types:
- Boolean: if set to
true, the element is always reverted. - String: if set to
"invalid", reverting occurs only when the draggable is not dropped on a droppable. If set to"valid"the opposite is true. - Function: a function that determines whether the element reverts to its starting position. The function must return
trueto restore the element.
Code example:
Initializes with the specified revert Options of draggable:
$( ".selector" ).draggable({ revert: true });
After initialization, get or set revert Option:
// getter var revert = $( ".selector" ).draggable( "option", "revert" );| false| onSelect | event | revertDuration | Number | The duration of the revert animation, in milliseconds. If// setter $( ".selector" ).draggable( "option", "revert", true );
revert The option is false it is ignored.
Code example:
Initializes with the specified revertDuration The draggable option:
$( ".selector" ).draggable({ revertDuration: 200 });
After initialization, get or set revertDuration Option:
// getter var revertDuration = $( ".selector" ).draggable( "option", "revertDuration" );| 500 | onSelect | event | scope | String | Used to group sets of draggable and droppable items, in addition to droppable's// setter $( ".selector" ).draggable( "option", "revertDuration", 200 );
accept Beyond the options. One with the same as droppable with scope A draggable with this value will be accepted by the droppable.
Code example:
Initializes with the specified scope Options of draggable:
$( ".selector" ).draggable({ scope: "tasks" });
After initialization, get or set scope Option:
// getter var scope = $( ".selector" ).draggable( "option", "scope" );| "default"| onSelect | event | scroll | Boolean | If set to// setter $( ".selector" ).draggable( "option", "scope", "tasks" );
true, the container scrolls automatically when dragging.
Code example:
Initializes with the specified scroll Options of draggable:
$( ".selector" ).draggable({ scroll: false });
After initialization, get or set scroll Option:
// getter var scroll = $( ".selector" ).draggable( "option", "scroll" );| true| onSelect | event | scrollSensitivity | Number | The distance in pixels from the edge of the viewport to scroll. The distance is relative to the pointer, not to the draggable. If// setter $( ".selector" ).draggable( "option", "scroll", false );
scroll The option is false it is ignored.
Code example:
Initializes with the specified scrollSensitivity The draggable option:
$( ".selector" ).draggable({ scrollSensitivity: 100 });
After initialization, get or set scrollSensitivity Option:
// getter var scrollSensitivity = $( ".selector" ).draggable( "option", "scrollSensitivity" );| 20| onSelect | event | scrollSpeed | Number | When the mouse pointer is moved within// setter $( ".selector" ).draggable( "option", "scrollSensitivity", 100 );
scrollSensitivity the speed at which the form scrolls when within a distance. If scroll The option is false it is ignored.
Code example:
Initializes with the specified scrollSpeed The draggable option:
$( ".selector" ).draggable({ scrollSpeed: 100 });
After initialization, get or set scrollSpeed Option:
// getter var scrollSpeed = $( ".selector" ).draggable( "option", "scrollSpeed" );| 20| onSelect | event | snap | Boolean or Selector | Whether the element should snap to other elements.// setter $( ".selector" ).draggable( "option", "scrollSpeed", 100 );
Multiple types are supported:
- Boolean: when set to
truewhen, the element snaps to other draggable elements. - SelectorA selector specifying which elements to snap to.
Code example:
Initializes with the specified snap The draggable option:
$( ".selector" ).draggable({ snap: true });
After initialization, get or set snap Option:
// getter var snap = $( ".selector" ).draggable( "option", "snap" );| false| onSelect | event | snapMode | String | Determines which edge of the snap target the draggable will snap to. If// setter $( ".selector" ).draggable( "option", "snap", true );
snap The option is false it is ignored. Possible values:"inner"、"outer"、"both"。
Code example:
Initializes with the specified snapMode Options of draggable:
$( ".selector" ).draggable({ snapMode: "inner" });
After initialization, get or set snapMode Option:
// getter var snapMode = $( ".selector" ).draggable( "option", "snapMode" );| "both"| onSelect | event | snapTolerance | Number | The distance from the edge of the target element to which snapping occurs, in pixels. If// setter $( ".selector" ).draggable( "option", "snapMode", "inner" );
snap The option is false it is ignored.
Code example:
Initializes with the specified snapTolerance Options of draggable:
$( ".selector" ).draggable({ snapTolerance: 30 });
After initialization, get or set snapTolerance Option:
// getter var snapTolerance = $( ".selector" ).draggable( "option", "snapTolerance" );| 20| onSelect | event | stack | Selector | Controls the z-index of the set of elements matching the selector, always bringing the currently dragged item to the front. Very useful in things like window managers.// setter $( ".selector" ).draggable( "option", "snapTolerance", 30 );
Code example:
Initializes with the specified stack Options of draggable:
$( ".selector" ).draggable({ stack: ".products" });
After initialization, get or set stack Option:
// getter var stack = $( ".selector" ).draggable( "option", "stack" );| false| onSelect | event | zIndex | Number | The z-index of the helper while it is being dragged.// setter $( ".selector" ).draggable( "option", "stack", ".products" );
Code example:
Initializes with the specified zIndex Options of draggable:
$( ".selector" ).draggable({ zIndex: 100 });
After initialization, get or set zIndex Option:
// getter var zIndex = $( ".selector" ).draggable( "option", "zIndex" );| false |// setter $( ".selector" ).draggable( "option", "zIndex", 100 );
| Method | Return | Description |
|---|---|---|
| destroy() | jQuery (plugin only) | Completely remove the draggable functionality. This returns the element to its pre-initialized state. |
Code example:
Call the destroy method:
$( ".selector" ).draggable( "destroy" );| onSelect | event | disable() | jQuery (plugin only) | Disable the draggable.
- This method accepts no arguments.
Code example:
Call the disable method:
$( ".selector" ).draggable( "disable" );| onSelect | event | enable() | jQuery (plugin only) | Enables the draggable.
- This method accepts no arguments.
Code example:
Call the enable method:
$( ".selector" ).draggable( "enable" );| onSelect | event | option( optionName ) | Object | Get the value currently associated with the specified `optionName`.
- optionName
Type: String
Description: The name of the option to get.
Code example:
Call this method:
var isDisabled = $( ".selector" ).draggable( "option", "disabled" );| onSelect | event | option() | PlainObject | Gets an object containing key/value pairs that represent the current draggable options hash.
- This method accepts no arguments.
Code example:
Call this method:
var options = $( ".selector" ).draggable( "option" );| onSelect | event | option( optionName, value ) | jQuery (plugin only) | Set the value of the draggable option associated with the specified `optionName`.
- optionName
Type: String
Description: The name of the option to set. - value
Type: Object
Description: The value to set for the option.
Code example:
Call this method:
$( ".selector" ).draggable( "option", "disabled", true );| onSelect | event | option( options ) | jQuery (plugin only) | Sets one or more options for the draggable.
- options
Type: Object
Description: The option value to set.
Code example:
Call this method:
$( ".selector" ).draggable( "option", { disabled: true } );
| onSelect | event | widget() | jQuery | Returns a jQuery object containing the draggable element.
- This method accepts no arguments.
Code example:
Call the widget method:
var widget = $( ".selector" ).draggable( "widget" );|
| Events | Type | Description |
|---|---|---|
| create( event, ui ) | dragcreate | Triggered when the draggable is created. |
Type: Event
Type: Object
Note:ui The object is empty; it is included here for consistency with other events.
Code example:
Initialize the draggable with the specified create callback:
$( ".selector" ).draggable({
create: function( event, ui ) {}
});
Bind an event listener to the dragcreate event:
$( ".selector" ).on( "dragcreate", function( event, ui ) {} );
| onSelect | event | drag( event, ui ) | drag | Triggered when the mouse moves during a drag.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being dragged. - position
Type: Object
Description: The current CSS position of the helper, for example{ top, left }object. - offset
Type: Object
Description: The current offset position of the helper, for example{ top, left }object.
- helper
Code example:
Initialize the draggable with the specified drag callback:
$( ".selector" ).draggable({
drag: function( event, ui ) {}
});
Bind an event listener to the drag event:
$( ".selector" ).on( "drag", function( event, ui ) {} );
| onSelect | event | start( event, ui ) | dragstart | Triggered when a drag starts.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being dragged. - position
Type: Object
Description: The current CSS position of the helper, for example{ top, left }object. - offset
Type: Object
Description: The current offset position of the helper, for example{ top, left }object.
- helper
Code example:
Initialize the draggable with the specified start callback:
$( ".selector" ).draggable({
start: function( event, ui ) {}
});
Bind an event listener to the dragstart event:
$( ".selector" ).on( "dragstart", function( event, ui ) {} );
| onSelect | event | stop( event, ui ) | dragstop | Triggered when a drag stops.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being dragged. - position
Type: Object
Description: The current CSS position of the helper, for example{ top, left }object. - offset
Type: Object
Description: The current offset position of the helper, for example{ top, left }object.
- helper
Code example:
Initialize the draggable with the specified stop callback:
$( ".selector" ).draggable({
stop: function( event, ui ) {}
});
Bind an event listener to the dragstop event:
$( ".selector" ).on( "dragstop", function( event, ui ) {} );
|
Example
A simple jQuery UI Draggable Widget.
可拖拽小部件(Draggable Widget)演示
请拖拽我!