jQuery UI API - Draggable Widget
Category
Usage
Description:Allows elements to be moved using the mouse.
Version added:1.0
Dependencies:
Notes:Makes the selected elements draggable with the mouse. If you don't just want to drag, but drag & drop, see thejQuery UI Droppable plugin, which provides a drop target for draggable elements.
Quick Navigation
| Options | Methods | Events |
|---|---|---|
| Options | Type | Description | Default |
|---|---|---|---|
| addClasses | Boolean | If set tofalse, it will preventui-draggablethe class from being added. When calling on hundreds of elements,.draggable()this setting is beneficial for performance optimization.Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ addClasses: false });
After initialization, get or set the // getter var addClasses = $( ".selector" ).draggable( "option", "addClasses" ); // setter $( ".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. Multiple types are supported:
Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ appendTo: "body" });
After initialization, get or set the // getter var appendTo = $( ".selector" ).draggable( "option", "appendTo" ); // setter $( ".selector" ).draggable( "option", "appendTo", "body" ); |
"parent" |
| axis | String | Constrains dragging to either the horizontal (x) or vertical (y) axis. Possible values:"x", "y"。Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ axis: "x" });
After initialization, get or set the // getter var axis = $( ".selector" ).draggable( "option", "axis" ); // setter $( ".selector" ).draggable( "option", "axis", "x" ); |
false |
| cancel | Selector | Prevents dragging from starting on specified elements. Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ cancel: ".title" });
After initialization, get or set the // getter var cancel = $( ".selector" ).draggable( "option", "cancel" ); // setter $( ".selector" ).draggable( "option", "cancel", ".title" ); |
"input, textarea, button, select, option" |
| connectToSortable | Selector | Allows the draggable to be dropped onto the specified sortable. If this option is used, a draggable can be dropped onto a sortable list and become part of it. Note:helperThe option must be set to"clone", so that it works better. Must includeSortable Widget.。Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ connectToSortable: "#my-sortable" });
After initialization, get or set the // getter var connectToSortable = $( ".selector" ).draggable( "option", "connectToSortable" ); // setter $( ".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 area. Multiple types are supported:
Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ containment: "parent" });
After initialization, get or set the // getter var containment = $( ".selector" ).draggable( "option", "containment" ); // setter $( ".selector" ).draggable( "option", "containment", "parent" ); |
false |
| cursor | String | The CSS cursor during the drag operation. Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ cursor: "crosshair" });
After initialization, get or set the // getter var cursor = $( ".selector" ).draggable( "option", "cursor" ); // setter $( ".selector" ).draggable( "option", "cursor", "crosshair" ); |
"auto" |
| cursorAt | Object | Sets the offset of the drag 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 specified
$( ".selector" ).draggable({ cursorAt: { left: 5 } });
After initialization, get or set the
// getter
var cursorAt = $( ".selector" ).draggable( "option", "cursorAt" );
// setter
$( ".selector" ).draggable( "option", "cursorAt", { left: 5 } );
|
false |
| delay | Number | The time in milliseconds after mousedown until dragging should start. This option can prevent unwanted dragging when clicking on an element. Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ delay: 300 });
After initialization, get or set the // getter var delay = $( ".selector" ).draggable( "option", "delay" ); // setter $( ".selector" ).draggable( "option", "delay", 300 ); |
0 |
| disabled | Boolean | If set totrue, then disables the draggable.Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ disabled: true });
After initialization, get or set the // getter var disabled = $( ".selector" ).draggable( "option", "disabled" ); // setter $( ".selector" ).draggable( "option", "disabled", true ); |
false |
| distance | Number | The distance in pixels the mouse must move after mousedown before dragging starts. This option can prevent unwanted dragging when clicking on an element. Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ distance: 10 });
After initialization, get or set the // getter var distance = $( ".selector" ).draggable( "option", "distance" ); // setter $( ".selector" ).draggable( "option", "distance", 10 ); |
1 |
| grid | Array | Snaps the drag helper to a grid, every x and y pixels. The array must be in the form[ x, y ]。Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ grid: [ 50, 20 ] });
After initialization, get or set the // getter var grid = $( ".selector" ).draggable( "option", "grid" ); // setter $( ".selector" ).draggable( "option", "grid", [ 50, 20 ] ); |
false |
| handle | Selector or Element | If specified, restricts dragging from starting unless mousedown occurs on the specified element. Only descendants of the draggable element are allowed to be dragged. Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ handle: "h2" });
After initialization, get or set the // getter var handle = $( ".selector" ).draggable( "option", "handle" ); // setter $( ".selector" ).draggable( "option", "handle", "h2" ); |
false |
| helper | String or Function() | Allows a helper element to be used for dragging display. Multiple types are supported:
Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ helper: "clone" });
After initialization, get or set the // getter var helper = $( ".selector" ).draggable( "option", "helper" ); // setter $( ".selector" ).draggable( "option", "helper", "clone" ); |
"original" |
| iframeFix | Boolean or Selector | Prevents iframes from capturing mousemove events during a drag. Useful in combination with thecursorAtoption, or when the mouse cursor is not over the helper.Multiple types are supported:
Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ iframeFix: true });
After initialization, get or set the // getter var iframeFix = $( ".selector" ).draggable( "option", "iframeFix" ); // setter $( ".selector" ).draggable( "option", "iframeFix", true ); |
false |
| opacity | Number | The opacity of the helper while being dragged. Code examples: Initialize the draggable with the specified
$( ".selector" ).draggable({ opacity: 0.35 });
After initialization, get or set the // getter var opacity = $( ".selector" ).draggable( "option", "opacity" ); // setter $( ".selector" ).draggable( "option", "opacity", 0.35 ); |
false |
| refreshPositions | Boolean | If set totrue, all droppable positions are calculated on every mousemove. Note: This solves highly dynamic scenarios, but significantly reduces performance.Code examples: Initialize with the specified
$( ".selector" ).draggable({ refreshPositions: true });
Get or set the // getter var refreshPositions = $( ".selector" ).draggable( "option", "refreshPositions" ); // setter $( ".selector" ).draggable( "option", "refreshPositions", true ); |
false |
| revert | Boolean or String or Function() | Whether the element reverts to its starting position when dragging stops. Supports multiple types:
Code examples: Initialize with the specified
$( ".selector" ).draggable({ revert: true });
Get or set the // getter var revert = $( ".selector" ).draggable( "option", "revert" ); // setter $( ".selector" ).draggable( "option", "revert", true ); |
false |
| revertDuration | Number | The duration of the revert animation, in milliseconds. If therevertoption isfalseit is ignored.Code examples: Initialize with the specified
$( ".selector" ).draggable({ revertDuration: 200 });
Get or set the // getter var revertDuration = $( ".selector" ).draggable( "option", "revertDuration" ); // setter $( ".selector" ).draggable( "option", "revertDuration", 200 ); |
500 |
| scope | String | Used to group paired draggable and droppable items, in addition to the droppable'sacceptoption. A draggable with the samescopevalue as the droppable will be accepted by that droppable.Code examples: Initialize with the specified
$( ".selector" ).draggable({ scope: "tasks" });
Get or set the // getter var scope = $( ".selector" ).draggable( "option", "scope" ); // setter $( ".selector" ).draggable( "option", "scope", "tasks" ); |
"default" |
| scroll | Boolean | If set totrue, the container automatically scrolls during dragging.Code examples: Initialize with the specified
$( ".selector" ).draggable({ scroll: false });
Get or set the // getter var scroll = $( ".selector" ).draggable( "option", "scroll" ); // setter $( ".selector" ).draggable( "option", "scroll", false ); |
true |
| scrollSensitivity | Number | The distance in pixels from the viewport edge from which scrolling should start. The distance is relative to the pointer, not to the draggable. If thescrolloption isfalseit is ignored.Code examples: Initialize with the specified
$( ".selector" ).draggable({ scrollSensitivity: 100 });
Get or set the // getter var scrollSensitivity = $( ".selector" ).draggable( "option", "scrollSensitivity" ); // setter $( ".selector" ).draggable( "option", "scrollSensitivity", 100 ); |
20 |
| scrollSpeed | Number | When the mouse pointer gets withinscrollSensitivitydistance, the speed at which the window scrolls. If thescrolloption isfalseit is ignored.Code examples: Initialize with the specified
$( ".selector" ).draggable({ scrollSpeed: 100 });
Get or set the // getter var scrollSpeed = $( ".selector" ).draggable( "option", "scrollSpeed" ); // setter $( ".selector" ).draggable( "option", "scrollSpeed", 100 ); |
20 |
| snap | Boolean or Selector | Whether the element snaps to other elements. Supports multiple types:
Code examples: Initialize with the specified
$( ".selector" ).draggable({ snap: true });
Get or set the // getter var snap = $( ".selector" ).draggable( "option", "snap" ); // setter $( ".selector" ).draggable( "option", "snap", true ); |
false |
| snapMode | String | Determines which edge of the snap target the draggable snaps to. If thesnapoption isfalseit is ignored. Possible values:"inner"、"outer"、"both"。Code examples: Initialize with the specified
$( ".selector" ).draggable({ snapMode: "inner" });
Get or set the // getter var snapMode = $( ".selector" ).draggable( "option", "snapMode" ); // setter $( ".selector" ).draggable( "option", "snapMode", "inner" ); |
"both" |
| snapTolerance | Number | The distance in pixels from the snap target edge at which snapping occurs. If thesnapoption isfalseit is ignored.Code examples: Initialize with the specified
$( ".selector" ).draggable({ snapTolerance: 30 });
Get or set the // getter var snapTolerance = $( ".selector" ).draggable( "option", "snapTolerance" ); // setter $( ".selector" ).draggable( "option", "snapTolerance", 30 ); |
20 |
| stack | Selector | Controls the z-index of the set of elements that match the selector, always in front of the currently dragged item. Useful for things like window managers. Code examples: Initialize with the specified
$( ".selector" ).draggable({ stack: ".products" });
Get or set the // getter var stack = $( ".selector" ).draggable( "option", "stack" ); // setter $( ".selector" ).draggable( "option", "stack", ".products" ); |
false |
| zIndex | Number | The z-index of the helper when being dragged. Code examples: Initialize with the specified
$( ".selector" ).draggable({ zIndex: 100 });
Get or set the // getter var zIndex = $( ".selector" ).draggable( "option", "zIndex" ); // setter $( ".selector" ).draggable( "option", "zIndex", 100 ); |
false |
| Methods | Returns | Description |
|---|---|---|
| destroy() | jQuery (plugin only) | Completely removes the draggable functionality. This returns the element to its pre-initialization state.
Code examples: Invoke the destroy method: $( ".selector" ).draggable( "destroy" ); |
| disable() | jQuery (plugin only) | Disables the draggable.
Code examples: Invoke the disable method: $( ".selector" ).draggable( "disable" ); |
| enable() | jQuery (plugin only) | Enables the draggable.
Code examples: Invoke the enable method: $( ".selector" ).draggable( "enable" ); |
| option( optionName ) | Object | Gets the value currently associated with the specifiedoptionNameoption.
Code examples: Invoke the method: var isDisabled = $( ".selector" ).draggable( "option", "disabled" ); |
| option() | PlainObject | Gets an object containing key/value pairs that represent the current draggable option hash.
Code examples: Invoke the method: var options = $( ".selector" ).draggable( "option" ); |
| option( optionName, value ) | jQuery (plugin only) | Sets the value of the draggable option associated with the specifiedoptionNameoption name.
Code examples: Invoke the method: $( ".selector" ).draggable( "option", "disabled", true ); |
| option( options ) | jQuery (plugin only) | Sets one or more options for the draggable.
Code examples: Invoke the method:
$( ".selector" ).draggable( "option", { disabled: true } );
|
| widget() | jQuery | Returns ajQueryobject containing the draggable element.
Code examples: Invoke the widget method: var widget = $( ".selector" ).draggable( "widget" ); |
| Events | Type | Description |
|---|---|---|
| create( event, ui ) | dragcreate | Triggered when the draggable is created.
Note: Code examples: 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 ) {} );
|
| drag( event, ui ) | drag | Triggered when the mouse moves during dragging.
Code examples: 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 ) {} );
|
| start( event, ui ) | dragstart | Triggered when dragging starts.
Code examples: 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 ) {} );
|
| stop( event, ui ) | dragstop | Triggered when dragging stops.
Code examples: 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 ) {} );
|
Examples
A simple jQuery UI Draggable Widget.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>可拖拽小部件(Draggable Widget)演示</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">请拖拽我!</div>
<script>
$( "#draggable" ).draggable();
</script>
</body>
</html>
Other extensions