jQuery UI API - Draggable Widget

Category

Interactions

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 specifiedaddClassesoption:

$( ".selector" ).draggable({ addClasses: false });
    

After initialization, get or set theaddClassesoption:

// 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:

  • 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: String"parent"will cause the helper to be a sibling of the draggable.

Code examples:

Initialize the draggable with the specifiedappendTooption:

$( ".selector" ).draggable({ appendTo: "body" });
    

After initialization, get or set theappendTooption:

// 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 specifiedaxisoption:

$( ".selector" ).draggable({ axis: "x" });
    

After initialization, get or set theaxisoption:

// 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 specifiedcanceloption:

$( ".selector" ).draggable({ cancel: ".title" });
    

After initialization, get or set thecanceloption:

// 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 specifiedconnectToSortableoption:

$( ".selector" ).draggable({ connectToSortable: "#my-sortable" });
    

After initialization, get or set theconnectToSortableoption:

// 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:

  • Selector: The draggable element will be contained within the bounds 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 within the bounds of the element.
  • String: Possible values:"parent"、"document"、"window"。
  • Array: An array, in the form[ x1, y1, x2, y2 ]defining the bounds of the element.

Code examples:

Initialize the draggable with the specifiedcontainmentoption:

$( ".selector" ).draggable({ containment: "parent" });
    

After initialization, get or set thecontainmentoption:

// 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 specifiedcursoroption:

$( ".selector" ).draggable({ cursor: "crosshair" });
    

After initialization, get or set thecursoroption:

// 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 specifiedcursorAtoption:

$( ".selector" ).draggable({ cursorAt: { left: 5 } });
    

After initialization, get or set thecursorAtoption:

// 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 specifieddelayoption:

$( ".selector" ).draggable({ delay: 300 });
    

After initialization, get or set thedelayoption:

// 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 specifieddisabledoption:

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

After initialization, get or set thedisabledoption:

// 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 specifieddistanceoption:

$( ".selector" ).draggable({ distance: 10 });
    

After initialization, get or set thedistanceoption:

// 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 specifiedgridoption:

$( ".selector" ).draggable({ grid: [ 50, 20 ] });
    

After initialization, get or set thegridoption:

// 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 specifiedhandleoption:

$( ".selector" ).draggable({ handle: "h2" });
    

After initialization, get or set thehandleoption:

// 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:

  • String: If set to"clone", the element will be cloned and the clone will be dragged.
  • Function: A function that will return a DOMElement to use while dragging.

Code examples:

Initialize the draggable with the specifiedhelperoption:

$( ".selector" ).draggable({ helper: "clone" });
    

After initialization, get or set thehelperoption:

// 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:

  • Boolean: When set totrue, a transparent overlay will be placed over all iframes on the page.
  • Selector: Any iframes matching the selector will be covered by a transparent overlay.

Code examples:

Initialize the draggable with the specifiediframeFixoption:

$( ".selector" ).draggable({ iframeFix: true });
    

After initialization, get or set theiframeFixoption:

// 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 specifiedopacityoption:

$( ".selector" ).draggable({ opacity: 0.35 });
    

After initialization, get or set theopacityoption:

// 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 specifiedrefreshPositionsoptions for the draggable:

$( ".selector" ).draggable({ refreshPositions: true });
    

Get or set therefreshPositionsoption after initialization:

// 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:

  • Boolean: If set totrue, the element always reverts.
  • String: If set to"invalid", revert occurs only when the draggable is not placed on a droppable; if set to"valid"the opposite occurs.
  • Function: A function that determines whether the element reverts to its starting position. The function must returntruefor the element to revert.

Code examples:

Initialize with the specifiedrevertoptions for the draggable:

$( ".selector" ).draggable({ revert: true });
    

Get or set therevertoption after initialization:

// 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 specifiedrevertDurationoptions for the draggable:

$( ".selector" ).draggable({ revertDuration: 200 });
    

Get or set therevertDurationoption after initialization:

// 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 specifiedscopeoptions for the draggable:

$( ".selector" ).draggable({ scope: "tasks" });
    

Get or set thescopeoption after initialization:

// 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 specifiedscrolloptions for the draggable:

$( ".selector" ).draggable({ scroll: false });
    

Get or set thescrolloption after initialization:

// 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 specifiedscrollSensitivityoptions for the draggable:

$( ".selector" ).draggable({ scrollSensitivity: 100 });
    

Get or set thescrollSensitivityoption after initialization:

// 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 specifiedscrollSpeedoptions for the draggable:

$( ".selector" ).draggable({ scrollSpeed: 100 });
    

Get or set thescrollSpeedoption after initialization:

// 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:

  • Boolean: When set totrue, the element snaps to other draggable elements.
  • Selector: A selector specifying which elements to snap to.

Code examples:

Initialize with the specifiedsnapoptions for the draggable:

$( ".selector" ).draggable({ snap: true });
    

Get or set thesnapoption after initialization:

// 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 specifiedsnapModeoptions for the draggable:

$( ".selector" ).draggable({ snapMode: "inner" });
    

Get or set thesnapModeoption after initialization:

// 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 specifiedsnapToleranceoptions for the draggable:

$( ".selector" ).draggable({ snapTolerance: 30 });
    

Get or set thesnapToleranceoption after initialization:

// 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 specifiedstackoptions for the draggable:

$( ".selector" ).draggable({ stack: ".products" });
    

Get or set thestackoption after initialization:

// 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 specifiedzIndexoptions for the draggable:

$( ".selector" ).draggable({ zIndex: 100 });
    

Get or set thezIndexoption after initialization:

// 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.
  • This method does not accept any arguments.

Code examples:

Invoke the destroy method:

$( ".selector" ).draggable( "destroy" );
    
disable() jQuery (plugin only) Disables the draggable.
  • This method does not accept any arguments.

Code examples:

Invoke the disable method:

$( ".selector" ).draggable( "disable" );
    
enable() jQuery (plugin only) Enables the draggable.
  • This method does not accept any arguments.

Code examples:

Invoke the enable method:

$( ".selector" ).draggable( "enable" );
    
option( optionName ) Object Gets the value currently associated with the specifiedoptionNameoption.
  • optionName
    Type: String
    Description: The name of the option to get.

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.
  • This method does not accept any arguments.

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.
  • optionName
    Type: String
    Description: The name of the option to set.
  • value
    Type: Object
    Description: The value to set for the option.

Code examples:

Invoke the method:

$( ".selector" ).draggable( "option", "disabled", true );
    
option( options ) jQuery (plugin only) Sets one or more options for the draggable.
  • options
    Type: Object
    Description: A map of option-value pairs to set.

Code examples:

Invoke the method:

$( ".selector" ).draggable( "option", { disabled: true } );
    
widget() jQuery Returns ajQueryobject containing the draggable element.
  • This method does not accept any arguments.

Code examples:

Invoke the widget method:

var widget = $( ".selector" ).draggable( "widget" );
    

Events Type Description
create( event, ui ) dragcreate Triggered when the draggable is created.
  • event
    Type: Event
  • ui
    Type: Object

Note:uiThe object is empty, but included for consistency with other events.

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.
  • event
    Type: Event
  • ui
    Type: Object
    • helper
      Type: jQuery
      Description: The jQuery object representing the draggable helper.
    • position
      Type: Object
      Description: The current CSS position of the helper, e.g., an{ top, left }object.
    • offset
      Type: Object
      Description: The current offset position of the helper, e.g., an{ top, left }object.

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.
  • event
    Type: Event
  • ui
    Type: Object
    • helper
      Type: jQuery
      Description: The jQuery object representing the draggable helper.
    • position
      Type: Object
      Description: The current CSS position of the helper, e.g., an{ top, left }object.
    • offset
      Type: Object
      Description: The current offset position of the helper, e.g., an{ top, left }object.

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.
  • event
    Type: Event
  • ui
    Type: Object
    • helper
      Type: jQuery
      Description: The jQuery object representing the helper being dragged.
    • position
      Type: Object
      Description: The current CSS position of the helper, such as{ top, left }object.
    • offset
      Type: Object
      Description: The current offset position of the helper, such as{ top, left }object.

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