jQuery UI API - Sortable Widget
Category
Usage
**Description:** Reorder elements in a list or grid using the mouse.
**Version added:** 1.0
Dependencies:
**Note:** The jQuery UI Sortable plugin makes selected elements sortable by dragging them with the mouse.
**Note:** To sort table rows,tbody they must be sortable, not table。
Quick navigation
| Option | Method | Events |
|---|---|---|
| Option | Type | Description | Default value |
|---|---|---|---|
| appendTo | jQuery, or Element, or Selector, or String | Where the helper moved by the mouse is appended while dragging (for example, to work around overlap/zIndex issues). |
支持多个类型:
"parent" will cause the helper to become a sibling of the sortable items.Code example:
Initialize the accordion with the specified appendTo options of the sortable:
$( ".selector" ).sortable({ appendTo: document.body });
After initialization, get or set the appendTo Option:
// getter var appendTo = $( ".selector" ).sortable( "option", "appendTo" );| "parent"| onSelect | event | axis | String | If defined, items can only be dragged horizontally or vertically. Possible values:// setter $( ".selector" ).sortable( "option", "appendTo", document.body );
"x", "y"。
Code example:
Initialize the accordion with the specified axis options of the sortable:
$( ".selector" ).sortable({ axis: "x" });
After initialization, get or set the axis Option:
// getter var axis = $( ".selector" ).sortable( "option", "axis" );| false| onSelect | event | cancel | Selector | Prevents sorting from starting on elements matching the selector.// setter $( ".selector" ).sortable( "option", "axis", "x" );
Code example:
Initialize the accordion with the specified cancel options of the sortable:
$( ".selector" ).sortable({ cancel: "a,button" });
After initialization, get or set the cancel Option:
// getter var cancel = $( ".selector" ).sortable( "option", "cancel" );| "input, textarea, button, select, option"| onSelect | event | connectWith | Selector | Selector for other sortable elements whose items should be connected with the list. This is a one-way relationship; if you want items to be connected in both directions, you must set it on both sortable elements// setter $( ".selector" ).sortable( "option", "cancel", "a,button" );
connectWith option.
Code example:
Initialize the accordion with the specified connectWith options of the sortable:
$( ".selector" ).sortable({ connectWith: "#shopping-cart" });
After initialization, get or set the connectWith Option:
// getter var connectWith = $( ".selector" ).sortable( "option", "connectWith" );| false| onSelect | event | containment | Element or Selector or String | Defines the boundary within which sortable items are constrained while dragging.// setter $( ".selector" ).sortable( "option", "connectWith", "#shopping-cart" );
Note: The element specified for containment must have a computed width and height (though it need not be set explicitly). For example, if you have float: left sortable children and specify containment: "parent", make sure the sortable/parent container has float: left , otherwise it will have height: 0, leading to undefined behavior.
Multiple types are supported:
- Element: An element to use as the container.
- Selector: A selector specifying an element to use as the container.
- String: A string identifying an element to use as the container. Possible values:
"parent"、"document"、"window"。
Code example:
Initialize the accordion with the specified containment options of the sortable:
$( ".selector" ).sortable({ containment: "parent" });
After initialization, get or set the containment Option:
// getter var containment = $( ".selector" ).sortable( "option", "containment" );| false| onSelect | event | cursor | String | Defines the cursor shown while sorting.// setter $( ".selector" ).sortable( "option", "containment", "parent" );
Code example:
Initialize the accordion with the specified cursor options of the sortable:
$( ".selector" ).sortable({ cursor: "move" });
After initialization, get or set the cursor Option:
// getter var cursor = $( ".selector" ).sortable( "option", "cursor" );| "auto"| onSelect | event | cursorAt | Object | Moves the sorted element or helper so the cursor always appears at the same position for the drag. Coordinates can be given as a hash with one or two of the following keys:// setter $( ".selector" ).sortable( "option", "cursor", "move" );
{ top, left, right, bottom }。
Code example:
Initialize the accordion with the specified cursorAt options of the sortable:
$( ".selector" ).sortable({ cursorAt: { left: 5 } });
After initialization, get or set the cursorAt Option:
// getter var cursorAt = $( ".selector" ).sortable( "option", "cursorAt" );| false| onSelect | event | delay | Integer | Time in milliseconds after the mouse is pressed until sorting starts. This option can prevent unwanted dragging when clicking on an element.// setter $( ".selector" ).sortable( "option", "cursorAt", { left: 5 } );
Code example:
Initialize the accordion with the specified delay options of the sortable:
$( ".selector" ).sortable({ delay: 150 });
After initialization, get or set the delay Option:
// getter var delay = $( ".selector" ).sortable( "option", "delay" );| 0 | onSelect | event | disabled | Boolean | If set to// setter $( ".selector" ).sortable( "option", "delay", 150 );
true, the sortable is disabled.
Code example:
Initialize the accordion with the specified disabled options of the sortable:
$( ".selector" ).sortable({ disabled: true });
After initialization, get or set the disabled Option:
// getter var disabled = $( ".selector" ).sortable( "option", "disabled" );| false| onSelect | event | distance | Number | The distance in pixels the mouse must move before sorting begins after being pressed. If specified, sorting will only start once the mouse has been dragged beyond the given distance. This option can be used to allow clicking on elements within a handle.// setter $( ".selector" ).sortable( "option", "disabled", true );
Code example:
Initialize the accordion with the specified distance options of the sortable:
$( ".selector" ).sortable({ distance: 5 });
After initialization, get or set the distance Option:
// getter var distance = $( ".selector" ).sortable( "option", "distance" );| 1| onSelect | event | dropOnEmpty | Boolean | If// setter $( ".selector" ).sortable( "option", "distance", 5 );
false, items from this sortable cannot be dropped on an empty connected sortable (see connectWith option is used).
Code example:
Initialize the accordion with the specified dropOnEmpty options of the sortable:
$( ".selector" ).sortable({ dropOnEmpty: false });
After initialization, get or set the dropOnEmpty Option:
// getter var dropOnEmpty = $( ".selector" ).sortable( "option", "dropOnEmpty" );| true | onSelect | event | forceHelperSize | Boolean | If// setter $( ".selector" ).sortable( "option", "dropOnEmpty", false );
true, forces the helper to have a size.
Code example:
Initialize the accordion with the specified forceHelperSize options of the sortable:
$( ".selector" ).sortable({ forceHelperSize: true });
After initialization, get or set the forceHelperSize Option:
// getter var forceHelperSize = $( ".selector" ).sortable( "option", "forceHelperSize" );| false| onSelect | event | forcePlaceholderSize | Boolean | If// setter $( ".selector" ).sortable( "option", "forceHelperSize", true );
true, forces the placeholder to have a size.
Code example:
Initialize the accordion with the specified forcePlaceholderSize options of the sortable:
$( ".selector" ).sortable({ forcePlaceholderSize: true });
After initialization, get or set the forcePlaceholderSize Option:
// getter var forcePlaceholderSize = $( ".selector" ).sortable( "option", "forcePlaceholderSize" );| false | onSelect | event | grid | Array | Snaps the sorted element or helper to a grid, every x and y pixels. The array form must be// setter $( ".selector" ).sortable( "option", "forcePlaceholderSize", true );
[ x, y ]。
Code example:
Initialize the accordion with the specified grid options of the sortable:
$( ".selector" ).sortable({ grid: [ 20, 10 ] });
After initialization, get or set the grid Option:
// getter var grid = $( ".selector" ).sortable( "option", "grid" );| false | onSelect | event | handle | Selector or Element | If specified, restricts sorting to start only on the designated elements.// setter $( ".selector" ).sortable( "option", "grid", [ 20, 10 ] );
Code example:
Initialize the accordion with the specified handle options of the sortable:
$( ".selector" ).sortable({ handle: ".handle" });
After initialization, get or set the handle Option:
// getter var handle = $( ".selector" ).sortable( "option", "handle" );| false| onSelect | event | helper | String or Function() | Allows a helper element to be used for drag display.// setter $( ".selector" ).sortable( "option", "handle", ".handle" );
Multiple types are supported:
- String: If set to
"clone", the element is cloned and the clone is dragged. - Function: A function that returns the DOMElement to use while dragging. The function receives the event and the element being sorted.
Code example:
Initialize the accordion with the specified helper options of the sortable:
$( ".selector" ).sortable({ helper: "clone" });
After initialization, get or set the helper Option:
// getter var helper = $( ".selector" ).sortable( "option", "helper" );| "original"| onSelect | event | items | Selector | Specifies which items within the element should be sortable.// setter $( ".selector" ).sortable( "option", "helper", "clone" );
Code example:
Initialize the accordion with the specified items options of the sortable:
$( ".selector" ).sortable({ items: "> li" });
After initialization, get or set the items Option:
// getter var items = $( ".selector" ).sortable( "option", "items" );| "> *" | onSelect | event | opacity | Number | The opacity of the helper while sorting. From// setter $( ".selector" ).sortable( "option", "items", "> li" );
0.01 to 1。
Code example:
Initialize the accordion with the specified opacity options of the sortable:
$( ".selector" ).sortable({ opacity: 0.5 });
After initialization, get or set the opacity Option:
// getter var opacity = $( ".selector" ).sortable( "option", "opacity" );| false| onSelect | event | placeholder | String | The class name to apply, otherwise a white space.// setter $( ".selector" ).sortable( "option", "opacity", 0.5 );
Code example:
Initialize the accordion with the specified placeholder options of the sortable:
$( ".selector" ).sortable({ placeholder: "sortable-placeholder" });
After initialization, get or set the placeholder Option:
// getter var placeholder = $( ".selector" ).sortable( "option", "placeholder" );| false | onSelect | event | revert | Boolean or Number | Whether a sortable item returns to its new position with a smooth animation.// setter $( ".selector" ).sortable( "option", "placeholder", "sortable-placeholder" );
Multiple types are supported:
- Boolean: When set to
true, the item will use an animation with the default duration. - Number: The duration of the animation in milliseconds.
Code example:
Initialize the accordion with the specified revert options of the sortable:
$( ".selector" ).sortable({ revert: true });
After initialization, get or set the revert Option:
// getter var revert = $( ".selector" ).sortable( "option", "revert" );| false| onSelect | event | scroll | Boolean | If set to// setter $( ".selector" ).sortable( "option", "revert", true );
true, the page scrolls when the edge is reached.
Code example:
Initialize the accordion with the specified scroll options of the sortable:
$( ".selector" ).sortable({ scroll: false });
After initialization, get or set the scroll Option:
// getter var scroll = $( ".selector" ).sortable( "option", "scroll" );| true| onSelect | event | scrollSensitivity | Number | Defines how close to the edge the mouse must be before scrolling starts.// setter $( ".selector" ).sortable( "option", "scroll", false );
Code example:
Initialize the accordion with the specified scrollSensitivity options of the sortable:
$( ".selector" ).sortable({ scrollSensitivity: 10 });
After initialization, get or set the scrollSensitivity Option:
// getter var scrollSensitivity = $( ".selector" ).sortable( "option", "scrollSensitivity" );| 20 | onSelect | event | scrollSpeed | Number | The speed at which the page scrolls when the mouse pointer comes within the// setter $( ".selector" ).sortable( "option", "scrollSensitivity", 10 );
scrollSensitivity distance. If the scroll option is false , then it is ignored.
Code example:
Initialize the accordion with the specified scrollSpeed options of the sortable:
$( ".selector" ).sortable({ scrollSpeed: 40 });
After initialization, get or set the scrollSpeed Option:
// getter var scrollSpeed = $( ".selector" ).sortable( "option", "scrollSpeed" );| 20 | onSelect | event | tolerance | String | Specifies the mode used to test whether an item overlaps another when it is moved. Possible values:// setter $( ".selector" ).sortable( "option", "scrollSpeed", 40 );
"intersect": The item overlaps another by at least 50%."pointer": The mouse pointer overlaps another item.
Code example:
Initialize the accordion with the specified tolerance options of the sortable:
$( ".selector" ).sortable({ tolerance: "pointer" });
After initialization, get or set the tolerance Option:
// getter var tolerance = $( ".selector" ).sortable( "option", "tolerance" );| "intersect"| onSelect | event | zIndex | Integer | The z-index of the element/helper while being sorted.// setter $( ".selector" ).sortable( "option", "tolerance", "pointer" );
Code example:
Initialize the accordion with the specified zIndex options of the sortable:
$( ".selector" ).sortable({ zIndex: 9999 });
After initialization, get or set the zIndex Option:
// getter var zIndex = $( ".selector" ).sortable( "option", "zIndex" );| 1000 |// setter $( ".selector" ).sortable( "option", "zIndex", 9999 );
| Method | Return | Description |
|---|---|---|
| cancel() | jQuery (plugin only) | Cancels any change currently in progress in the sortable and restores the items to their previous state. Very useful in the stop and receive callbacks. |
Code example:
Invoke the cancel method:
$( ".selector" ).sortable( "cancel" );| onSelect | event | destroy() | jQuery (plugin only) | Completely removes the sortable functionality. This returns the element to its pre-initialization state.
- This method accepts no arguments.
Code example:
Invoke the destroy method:
$( ".selector" ).sortable( "destroy" );| onSelect | event | disable() | jQuery (plugin only) | Disables the sortable.
- This method accepts no arguments.
Code example:
Invoke the disable method:
$( ".selector" ).sortable( "disable" );| onSelect | event | enable() | jQuery (plugin only) | Enables the sortable.
- This method accepts no arguments.
Code example:
Invoke the enable method:
$( ".selector" ).sortable( "enable" );| onSelect | event | option( optionName ) | Object | Gets the value currently associated with the specified `optionName`.
- optionName
Type: String
Description: the name of the option to get.
Code example:
Invoke the method:
var isDisabled = $( ".selector" ).sortable( "option", "disabled" );| onSelect | event | option() | PlainObject | Gets an object containing key/value pairs representing the current draggable options hash.
- This method accepts no arguments.
Code example:
Invoke the method:
var options = $( ".selector" ).sortable( "option" );| onSelect | event | option( optionName, value ) | jQuery (plugin only) | Sets the value of the sortable 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:
Invoke the method:
$( ".selector" ).sortable( "option", "disabled", true );| onSelect | event | option( options ) | jQuery (plugin only) | Sets one or more options for the sortable.
- options
Type: Object
Description: the option-value pairs to set.
Code example:
Invoke the method:
$( ".selector" ).sortable( "option", { disabled: true } );
| onSelect | event | refresh() | jQuery (plugin only) | Refreshes the sortable items. Triggers a reload of all sortable items, causing new items to be recognized.
- This method accepts no arguments.
Code example:
Invoke the refresh method:
$( ".selector" ).sortable( "refresh" );| onSelect | event | refreshPositions() | jQuery (plugin only) | Refreshes the cached positions of sortable items. Calling this method refreshes the cached item positions of all sortables.
- This method accepts no arguments.
Code example:
Invoke the refreshPositions method:
$( ".selector" ).sortable( "refreshPositions" );| onSelect | event | serialize( options ) | String | Serializes the `id` of the sortable items into a string that can be submitted by a form/ajax. Calling this method produces a hash that can be appended to any url to easily submit a new item order back to the server.
By default, it works through each item's id , with ids of the form "setname_number", and it produces a "setname[]=number&setname[]=number" hash.
Note: If serialization returns an empty string, make sure the id attribute contains an underscore (_). The form must be "set_number". For example, a id attribute to "foo_1"、"foo_5"、"foo_2" 3-element list will serialize to "foo[]=1&foo[]=5&foo[]=2". You can use an underscore (_), equals sign (=), or hyphen (-) to separate the set from the number. For example,"foo=1"、"foo-1"、"foo_1" all serialize to "foo[]=1"。
- options
Type: Object
options to customize serialization.- key(default:
属性中分隔符前面的部分)
Type: String
Description: Replacespart1[]with the specified value. - attribute(default:
"id")
Type: String
Description: The name of the attribute to use for the value. - expression(default:
/(.+)[-=_](.+)/)
Type: RegExp
Description: A regular expression used to split the attribute value into two parts, key and value.
- key(default:
Code example:
Invoke the serialize method:
var sorted = $( ".selector" ).sortable( "serialize", { key: "sort" } );
| onSelect | event | toArray( options ) | Array | Serializes the `id` of the sortable items into an array of strings.
- options
Type: Object
options to customize serialization.- attribute(default:
"id")
Type: String
Description: The name of the attribute to use for the value.
- attribute(default:
Code example:
Invoke the toArray method:
var sortedIDs = $( ".selector" ).sortable( "toArray" );| onSelect | event | widget() | jQuery | Returns a `jQuery` object containing the sortable element.
- This method accepts no arguments.
Code example:
Invoke the widget method:
var widget = $( ".selector" ).sortable( "widget" );|
| Events | Type | Description |
|---|---|---|
| activate( event, ui ) | sortactivate | Triggered when using connected lists; each connected list receives it when a drag starts. |
Type: Event
Type: Object
- helper
Type: jQuery
Description: A jQuery object representing the helper being sorted. - item
Type: jQuery
Description: A jQuery object representing the element currently being dragged. - offset
Type: Object
Description: The current absolute position of the helper, expressed as{ top, left }。 - position
Type: Object
Description: The current position of the helper, expressed as{ top, left }。 - originalPosition
Type: Object
Description: The original position of the element, expressed as{ top, left }。 - sender
Type: jQuery
Description: If moved from one sortable to another, the sortable the item came from. - placeholder
Type: jQuery
Description: A jQuery object representing the element used as the placeholder.
Code example:
Initialize the sortable with the specified activate callback:
$( ".selector" ).sortable({
activate: function( event, ui ) {}
});
Bind an event listener to the sortactivate event:
$( ".selector" ).on( "sortactivate", function( event, ui ) {} );
| onSelect | event | beforeStop( event, ui ) | sortbeforestop | Triggered when sorting stops, except when the placeholder/helper is still available.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being sorted. - item
Type: jQuery
Description: A jQuery object representing the element currently being dragged. - offset
Type: Object
Description: The current absolute position of the helper, expressed as{ top, left }。 - position
Type: Object
Description: The current position of the helper, expressed as{ top, left }。 - originalPosition
Type: Object
Description: The original position of the element, expressed as{ top, left }。 - sender
Type: jQuery
Description: If moved from one sortable to another, the sortable the item came from. - placeholder
Type: jQuery
Description: A jQuery object representing the element used as the placeholder.
- helper
Code example:
Initialize the sortable with the specified beforeStop callback:
$( ".selector" ).sortable({
beforeStop: function( event, ui ) {}
});
Bind an event listener to the sortbeforestop event:
$( ".selector" ).on( "sortbeforestop", function( event, ui ) {} );
| onSelect | event | change( event, ui ) | sortchange | Triggered during sorting, except when the DOM position changes.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being sorted. - item
Type: jQuery
Description: A jQuery object representing the element currently being dragged. - offset
Type: Object
Description: The current absolute position of the helper, expressed as{ top, left }。 - position
Type: Object
Description: The current position of the helper, expressed as{ top, left }。 - originalPosition
Type: Object
Description: The original position of the element, expressed as{ top, left }。 - sender
Type: jQuery
Description: If moved from one sortable to another, the sortable the item came from. - placeholder
Type: jQuery
Description: A jQuery object representing the element used as the placeholder.
- helper
Code example:
Initialize the sortable with the specified change callback:
$( ".selector" ).sortable({
change: function( event, ui ) {}
});
Bind an event listener to the sortchange event:
$( ".selector" ).on( "sortchange", function( event, ui ) {} );
| onSelect | event | create( event, ui ) | sortcreate | Triggered when the droppable is created.
- event
Type: Event - ui
Type: Object
Note:ui The object is empty; it is included here for consistency with other events.
Code example:
Initialize the sortable with the specified create callback:
$( ".selector" ).sortable({
create: function( event, ui ) {}
});
Bind an event listener to the sortcreate event:
$( ".selector" ).on( "sortcreate", function( event, ui ) {} );
| onSelect | event | deactivate( event, ui ) | sortdeactivate | Triggered when sorting stops; the event propagates to all possible connected lists.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being sorted. - item
Type: jQuery
Description: A jQuery object representing the element currently being dragged. - offset
Type: Object
Description: The current absolute position of the helper, expressed as{ top, left }。 - position
Type: Object
Description: The current position of the helper, expressed as{ top, left }。 - originalPosition
Type: Object
Description: The original position of the element, expressed as{ top, left }。 - sender
Type: jQuery
Description: If moved from one sortable to another, the sortable the item came from. - placeholder
Type: jQuery
Description: A jQuery object representing the element used as the placeholder.
- helper
Code example:
Initialize the sortable with the specified deactivate callback:
$( ".selector" ).sortable({
deactivate: function( event, ui ) {}
});
Bind an event listener to the sortdeactivate event:
$( ".selector" ).on( "sortdeactivate", function( event, ui ) {} );
| onSelect | event | out( event, ui ) | sortout | Triggered when a sortable item is removed from a sortable list.
Note: This event is also triggered when a sortable item is cancelled.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being sorted. - item
Type: jQuery
Description: A jQuery object representing the element currently being dragged. - offset
Type: Object
Description: The current absolute position of the helper, expressed as{ top, left }。 - position
Type: Object
Description: The current position of the helper, expressed as{ top, left }。 - originalPosition
Type: Object
Description: The original position of the element, expressed as{ top, left }。 - sender
Type: jQuery
Description: If moved from one sortable to another, the sortable the item came from. - placeholder
Type: jQuery
Description: A jQuery object representing the element used as the placeholder.
- helper
Code example:
Initialize the sortable with the specified out callback:
$( ".selector" ).sortable({
out: function( event, ui ) {}
});
Bind an event listener to the sortout event:
$( ".selector" ).on( "sortout", function( event, ui ) {} );
| onSelect | event | over( event, ui ) | sortover | Triggered when a sortable item is moved into a sortable list.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being sorted. - item
Type: jQuery
Description: A jQuery object representing the element currently being dragged. - offset
Type: Object
Description: The current absolute position of the helper, expressed as{ top, left }。 - position
Type: Object
Description: The current position of the helper, expressed as{ top, left }。 - originalPosition
Type: Object
Description: The original position of the element, expressed as{ top, left }。 - sender
Type: jQuery
Description: If moved from one sortable to another, the sortable the item came from. - placeholder
Type: jQuery
Description: A jQuery object representing the element used as the placeholder.
- helper
Code example:
Initialize the sortable with the specified over callback:
$( ".selector" ).sortable({
out: function( event, ui ) {}
});
Bind an event listener to the sortover event:
$( ".selector" ).on( "sortout", function( event, ui ) {} );
| onSelect | event | receive( event, ui ) | sortreceive | Triggered when an item from a connected sortable list is dropped into another list. The latter is the event target.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being sorted. - item
Type: jQuery
Description: A jQuery object representing the element currently being dragged. - offset
Type: Object
Description: The current absolute position of the helper, expressed as{ top, left }。 - position
Type: Object
Description: The current position of the helper, expressed as{ top, left }。 - originalPosition
Type: Object
Description: The original position of the element, expressed as{ top, left }。 - sender
Type: jQuery
Description: If moved from one sortable to another, the sortable the item came from. - placeholder
Type: jQuery
Description: A jQuery object representing the element used as the placeholder.
- helper
Code example:
Initialize the sortable with the specified receive callback:
$( ".selector" ).sortable({
receive: function( event, ui ) {}
});
Bind an event listener to the sortreceive event:
$( ".selector" ).on( "sortreceive", function( event, ui ) {} );
| onSelect | event | remove( event, ui ) | sortremove | Triggered when an item from a connected sortable list is dropped into another list. The former is the event target.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being sorted. - item
Type: jQuery
Description: A jQuery object representing the element currently being dragged. - offset
Type: Object
Description: The current absolute position of the helper, expressed as{ top, left }。 - position
Type: Object
Description: The current position of the helper, expressed as{ top, left }。 - originalPosition
Type: Object
Description: The original position of the element, expressed as{ top, left }。 - placeholder
Type: jQuery
Description: A jQuery object representing the element used as the placeholder.
- helper
Code example:
Initialize the sortable with the specified remove callback:
$( ".selector" ).sortable({
remove: function( event, ui ) {}
});
Bind an event listener to the sortremove event:
$( ".selector" ).on( "sortremove", function( event, ui ) {} );
| onSelect | event | sort( event, ui ) | sort | Triggered during sorting.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being sorted. - item
Type: jQuery
Description: A jQuery object representing the element currently being dragged. - offset
Type: Object
Description: The current absolute position of the helper, expressed as{ top, left }。 - position
Type: Object
Description: The current position of the helper, expressed as{ top, left }。 - originalPosition
Type: Object
Description: The original position of the element, expressed as{ top, left }。 - sender
Type: jQuery
Description: If moved from one sortable to another, the sortable the item came from. - placeholder
Type: jQuery
Description: A jQuery object representing the element used as the placeholder.
- helper
Code example:
Initialize the sortable with the specified sort callback:
$( ".selector" ).sortable({
sort: function( event, ui ) {}
});
Bind an event listener to the sort event:
$( ".selector" ).on( "sort", function( event, ui ) {} );
| onSelect | event | start( event, ui ) | sortstart | Triggered when sorting starts.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being sorted. - item
Type: jQuery
Description: A jQuery object representing the element currently being dragged. - offset
Type: Object
Description: The current absolute position of the helper, expressed as{ top, left }。 - position
Type: Object
Description: The current position of the helper, expressed as{ top, left }。 - originalPosition
Type: Object
Description: The original position of the element, expressed as{ top, left }。 - sender
Type: jQuery
Description: If moved from one sortable to another, the sortable the item came from. - placeholder
Type: jQuery
Description: A jQuery object representing the element used as the placeholder.
- helper
Code example:
Initialize the sortable with the specified start callback:
$( ".selector" ).sortable({
start: function( event, ui ) {}
});
Bind an event listener to the sortstart event:
$( ".selector" ).on( "sortstart", function( event, ui ) {} );
| onSelect | event | stop( event, ui ) | sortstop | Triggered when sorting stops.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being sorted. - item
Type: jQuery
Description: A jQuery object representing the element currently being dragged. - offset
Type: Object
Description: The current absolute position of the helper, expressed as{ top, left }。 - position
Type: Object
Description: The current position of the helper, expressed as{ top, left }。 - originalPosition
Type: Object
Description: The original position of the element, expressed as{ top, left }。 - sender
Type: jQuery
Description: If moved from one sortable to another, the sortable the item came from. - placeholder
Type: jQuery
Description: A jQuery object representing the element used as the placeholder.
- helper
Code example:
Initialize the sortable with the specified stop callback:
$( ".selector" ).sortable({
stop: function( event, ui ) {}
});
Bind an event listener to the sortstop event:
$( ".selector" ).on( "sortstop", function( event, ui ) {} );
| onSelect | event | update( event, ui ) | sortupdate | Triggered when the user stops sorting and the DOM position has changed.
- event
Type: Event - ui
Type: Object- helper
Type: jQuery
Description: A jQuery object representing the helper being sorted. - item
Type: jQuery
Description: A jQuery object representing the element currently being dragged. - offset
Type: Object
Description: The current absolute position of the helper, expressed as{ top, left }。 - position
Type: Object
Description: The current position of the helper, expressed as{ top, left }。 - originalPosition
Type: Object
Description: The original position of the element, expressed as{ top, left }。 - sender
Type: jQuery
Description: If moved from one sortable to another, the sortable the item came from. - placeholder
Type: jQuery
Description: A jQuery object representing the element used as the placeholder.
- helper
Code example:
Initialize the sortable with the specified update callback:
$( ".selector" ).sortable({
update: function( event, ui ) {}
});
Bind an event listener to the sortupdate event:
$( ".selector" ).on( "sortupdate", function( event, ui ) {} );
|
Example
A simple jQuery UI Sortable widget.
可排序小部件(Sortable Widget)演示
- Item 1
- Item 2
- Item 3
- Item 4
- Item 5