| 1 |
# Sortable [](https://opencollective.com/Sortable](https://opencollective.com/Sortable](https://opencollective.com/Sortable) [](https://circleci.com/gh/SortableJS/Sortable](https://circleci.com/gh/SortableJS/Sortable](https://circleci.com/gh/SortableJS/Sortable) [](https://deepscan.io/dashboard#view=project&tid=3901&pid=5666&bid=43977](https://deepscan.io/dashboard#view=project&tid=3901&pid=5666&bid=43977](https://deepscan.io/dashboard#view=project&tid=3901&pid=5666&bid=43977) [](https://www.jsdelivr.com/package/npm/sortablejs](https://www.jsdelivr.com/package/npm/sortablejs](https://www.jsdelivr.com/package/npm/sortablejs) [](https://www.npmjs.com/package/sortablejs](https://www.npmjs.com/package/sortablejs](https://www.npmjs.com/package/sortablejs) |
| 2 |
|
| 3 |
Sortable is a JavaScript library for reorderable drag-and-drop lists. |
| 4 |
|
| 5 |
Demo: http://sortablejs.github.io/Sortable/ |
| 6 |
|
| 7 |
[](https://saucelabs.com/<img width="250px" src="https://raw.githubusercontent.com/SortableJS/Sortable/HEAD/st/saucelabs.svg?sanitize=true">](https://saucelabs.com/](https://saucelabs.com/) |
| 8 |
|
| 9 |
## Features |
| 10 |
|
| 11 |
* Supports touch devices and [](http://caniuse.com/#search=dragmodern](http://caniuse.com/#search=drag](http://caniuse.com/#search=drag) browsers (including IE9) |
| 12 |
* Can drag from one list to another or within the same list |
| 13 |
* CSS animation when moving items |
| 14 |
* Supports drag handles *and selectable text* (better than voidberg's html5sortable) |
| 15 |
* Smart auto-scrolling |
| 16 |
* Advanced swap detection |
| 17 |
* Smooth animations |
| 18 |
* [](https://github.com/SortableJS/Sortable/tree/master/plugins/MultiDragMulti-drag](https://github.com/SortableJS/Sortable/tree/master/plugins/MultiDrag](https://github.com/SortableJS/Sortable/tree/master/plugins/MultiDrag) support |
| 19 |
* Support for CSS transforms |
| 20 |
* Built using native HTML5 drag and drop API |
| 21 |
* Supports |
| 22 |
* [](https://github.com/SortableJS/meteor-sortablejsMeteor](https://github.com/SortableJS/meteor-sortablejs](https://github.com/SortableJS/meteor-sortablejs) |
| 23 |
* Angular |
| 24 |
* [](https://github.com/SortableJS/angular-sortablejs2.0+](https://github.com/SortableJS/angular-sortablejs](https://github.com/SortableJS/angular-sortablejs) |
| 25 |
* [](https://github.com/SortableJS/angular-legacy-sortablejs1.*](https://github.com/SortableJS/angular-legacy-sortablejs](https://github.com/SortableJS/angular-legacy-sortablejs) |
| 26 |
* React |
| 27 |
* [](https://github.com/SortableJS/react-sortablejsES2015+](https://github.com/SortableJS/react-sortablejs](https://github.com/SortableJS/react-sortablejs) |
| 28 |
* [](https://github.com/SortableJS/react-mixin-sortablejsMixin](https://github.com/SortableJS/react-mixin-sortablejs](https://github.com/SortableJS/react-mixin-sortablejs) |
| 29 |
* [](https://github.com/SortableJS/knockout-sortablejsKnockout](https://github.com/SortableJS/knockout-sortablejs](https://github.com/SortableJS/knockout-sortablejs) |
| 30 |
* [](https://github.com/SortableJS/polymer-sortablejsPolymer](https://github.com/SortableJS/polymer-sortablejs](https://github.com/SortableJS/polymer-sortablejs) |
| 31 |
* [](https://github.com/SortableJS/Vue.DraggableVue](https://github.com/SortableJS/Vue.Draggable](https://github.com/SortableJS/Vue.Draggable) |
| 32 |
* [](https://github.com/SortableJS/ember-sortablejsEmber](https://github.com/SortableJS/ember-sortablejs](https://github.com/SortableJS/ember-sortablejs) |
| 33 |
* Supports any CSS library, e.g. [](#bsBootstrap](#bs](#bs) |
| 34 |
* Simple API |
| 35 |
* Support for [](#pluginsplugins](#plugins](#plugins) |
| 36 |
* [](#cdnCDN](#cdn](#cdn) |
| 37 |
* No jQuery required (but there is [](https://github.com/SortableJS/jquery-sortablejssupport](https://github.com/SortableJS/jquery-sortablejs](https://github.com/SortableJS/jquery-sortablejs)) |
| 38 |
* Typescript definitions at `@types/sortablejs` |
| 39 |
|
| 40 |
|
| 41 |
<br/> |
| 42 |
|
| 43 |
|
| 44 |
### Articles |
| 45 |
|
| 46 |
* [](https://github.com/SortableJS/Sortable/wiki/Dragging-Multiple-Items-in-SortableDragging Multiple Items in Sortable](https://github.com/SortableJS/Sortable/wiki/Dragging-Multiple-Items-in-Sortable](https://github.com/SortableJS/Sortable/wiki/Dragging-Multiple-Items-in-Sortable) (April 26, 2019) |
| 47 |
* [](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-DirectionSwap Thresholds and Direction](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction) (December 2, 2018) |
| 48 |
* [](https://github.com/SortableJS/Sortable/wiki/Sortable-v1.0-—-New-capabilities/Sortable v1.0 — New capabilities](https://github.com/SortableJS/Sortable/wiki/Sortable-v1.0-—-New-capabilities/](https://github.com/SortableJS/Sortable/wiki/Sortable-v1.0-—-New-capabilities/) (December 22, 2014) |
| 49 |
* [](https://github.com/SortableJS/Sortable/wiki/Sorting-with-the-help-of-HTML5-Drag'n'Drop-API/Sorting with the help of HTML5 Drag'n'Drop API](https://github.com/SortableJS/Sortable/wiki/Sorting-with-the-help-of-HTML5-Drag'n'Drop-API/](https://github.com/SortableJS/Sortable/wiki/Sorting-with-the-help-of-HTML5-Drag'n'Drop-API/) (December 23, 2013) |
| 50 |
|
| 51 |
<br/> |
| 52 |
|
| 53 |
### Getting Started |
| 54 |
|
| 55 |
Install with NPM: |
| 56 |
```bash |
| 57 |
npm install sortablejs --save |
| 58 |
``` |
| 59 |
|
| 60 |
Install with Bower: |
| 61 |
```bash |
| 62 |
bower install --save sortablejs |
| 63 |
``` |
| 64 |
|
| 65 |
Import into your project: |
| 66 |
```js |
| 67 |
// Default SortableJS |
| 68 |
import Sortable from 'sortablejs'; |
| 69 |
|
| 70 |
// Core SortableJS (without default plugins) |
| 71 |
import Sortable from 'sortablejs/modular/sortable.core.esm.js'; |
| 72 |
|
| 73 |
// Complete SortableJS (with all plugins) |
| 74 |
import Sortable from 'sortablejs/modular/sortable.complete.esm.js'; |
| 75 |
``` |
| 76 |
|
| 77 |
Cherrypick plugins: |
| 78 |
```js |
| 79 |
// Cherrypick extra plugins |
| 80 |
import Sortable, { MultiDrag, Swap } from 'sortablejs'; |
| 81 |
|
| 82 |
Sortable.mount(new MultiDrag(), new Swap()); |
| 83 |
|
| 84 |
|
| 85 |
// Cherrypick default plugins |
| 86 |
import Sortable, { AutoScroll } from 'sortablejs/modular/sortable.core.esm.js'; |
| 87 |
|
| 88 |
Sortable.mount(new AutoScroll()); |
| 89 |
``` |
| 90 |
|
| 91 |
|
| 92 |
--- |
| 93 |
|
| 94 |
|
| 95 |
### Usage |
| 96 |
```html |
| 97 |
<ul id="items"> |
| 98 |
<li>item 1</li> |
| 99 |
<li>item 2</li> |
| 100 |
<li>item 3</li> |
| 101 |
</ul> |
| 102 |
``` |
| 103 |
|
| 104 |
```js |
| 105 |
var el = document.getElementById('items'); |
| 106 |
var sortable = Sortable.create(el); |
| 107 |
``` |
| 108 |
|
| 109 |
You can use any element for the list and its elements, not just `ul`/`li`. Here is an [](https://jsbin.com/visimub/edit?html,js,outputexample with `div`s](https://jsbin.com/visimub/edit?html,js,output](https://jsbin.com/visimub/edit?html,js,output). |
| 110 |
|
| 111 |
|
| 112 |
--- |
| 113 |
|
| 114 |
|
| 115 |
### Options |
| 116 |
```js |
| 117 |
var sortable = new Sortable(el, { |
| 118 |
group: "name", // or { name: "...", pull: [true, false, 'clone', array], put: [true, false, array] } |
| 119 |
sort: true, // sorting inside list |
| 120 |
delay: 0, // time in milliseconds to define when the sorting should start |
| 121 |
delayOnTouchOnly: false, // only delay if user is using touch |
| 122 |
touchStartThreshold: 0, // px, how many pixels the point should move before cancelling a delayed drag event |
| 123 |
disabled: false, // Disables the sortable if set to true. |
| 124 |
store: null, // @see Store |
| 125 |
animation: 150, // ms, animation speed moving items when sorting, `0` — without animation |
| 126 |
easing: "cubic-bezier(1, 0, 0, 1)", // Easing for animation. Defaults to null. See https://easings.net/ for examples. |
| 127 |
handle: ".my-handle", // Drag handle selector within list items |
| 128 |
filter: ".ignore-elements", // Selectors that do not lead to dragging (String or Function) |
| 129 |
preventOnFilter: true, // Call `event.preventDefault()` when triggered `filter` |
| 130 |
draggable: ".item", // Specifies which items inside the element should be draggable |
| 131 |
|
| 132 |
dataIdAttr: 'data-id', // HTML attribute that is used by the `toArray()` method |
| 133 |
|
| 134 |
ghostClass: "sortable-ghost", // Class name for the drop placeholder |
| 135 |
chosenClass: "sortable-chosen", // Class name for the chosen item |
| 136 |
dragClass: "sortable-drag", // Class name for the dragging item |
| 137 |
|
| 138 |
swapThreshold: 1, // Threshold of the swap zone |
| 139 |
invertSwap: false, // Will always use inverted swap zone if set to true |
| 140 |
invertedSwapThreshold: 1, // Threshold of the inverted swap zone (will be set to swapThreshold value by default) |
| 141 |
direction: 'horizontal', // Direction of Sortable (will be detected automatically if not given) |
| 142 |
|
| 143 |
forceFallback: false, // ignore the HTML5 DnD behaviour and force the fallback to kick in |
| 144 |
|
| 145 |
fallbackClass: "sortable-fallback", // Class name for the cloned DOM Element when using forceFallback |
| 146 |
fallbackOnBody: false, // Appends the cloned DOM Element into the Document's Body |
| 147 |
fallbackTolerance: 0, // Specify in pixels how far the mouse should move before it's considered as a drag. |
| 148 |
|
| 149 |
dragoverBubble: false, |
| 150 |
removeCloneOnHide: true, // Remove the clone element when it is not showing, rather than just hiding it |
| 151 |
emptyInsertThreshold: 5, // px, distance mouse must be from empty sortable to insert drag element into it |
| 152 |
|
| 153 |
|
| 154 |
setData: function (/** DataTransfer */dataTransfer, /** HTMLElement*/dragEl) { |
| 155 |
dataTransfer.setData('Text', dragEl.textContent); // `dataTransfer` object of HTML5 DragEvent |
| 156 |
}, |
| 157 |
|
| 158 |
// Element is chosen |
| 159 |
onChoose: function (/**Event*/evt) { |
| 160 |
evt.oldIndex; // element index within parent |
| 161 |
}, |
| 162 |
|
| 163 |
// Element is unchosen |
| 164 |
onUnchoose: function(/**Event*/evt) { |
| 165 |
// same properties as onEnd |
| 166 |
}, |
| 167 |
|
| 168 |
// Element dragging started |
| 169 |
onStart: function (/**Event*/evt) { |
| 170 |
evt.oldIndex; // element index within parent |
| 171 |
}, |
| 172 |
|
| 173 |
// Element dragging ended |
| 174 |
onEnd: function (/**Event*/evt) { |
| 175 |
var itemEl = evt.item; // dragged HTMLElement |
| 176 |
evt.to; // target list |
| 177 |
evt.from; // previous list |
| 178 |
evt.oldIndex; // element's old index within old parent |
| 179 |
evt.newIndex; // element's new index within new parent |
| 180 |
evt.oldDraggableIndex; // element's old index within old parent, only counting draggable elements |
| 181 |
evt.newDraggableIndex; // element's new index within new parent, only counting draggable elements |
| 182 |
evt.clone // the clone element |
| 183 |
evt.pullMode; // when item is in another sortable: `"clone"` if cloning, `true` if moving |
| 184 |
}, |
| 185 |
|
| 186 |
// Element is dropped into the list from another list |
| 187 |
onAdd: function (/**Event*/evt) { |
| 188 |
// same properties as onEnd |
| 189 |
}, |
| 190 |
|
| 191 |
// Changed sorting within list |
| 192 |
onUpdate: function (/**Event*/evt) { |
| 193 |
// same properties as onEnd |
| 194 |
}, |
| 195 |
|
| 196 |
// Called by any change to the list (add / update / remove) |
| 197 |
onSort: function (/**Event*/evt) { |
| 198 |
// same properties as onEnd |
| 199 |
}, |
| 200 |
|
| 201 |
// Element is removed from the list into another list |
| 202 |
onRemove: function (/**Event*/evt) { |
| 203 |
// same properties as onEnd |
| 204 |
}, |
| 205 |
|
| 206 |
// Attempt to drag a filtered element |
| 207 |
onFilter: function (/**Event*/evt) { |
| 208 |
var itemEl = evt.item; // HTMLElement receiving the `mousedown|tapstart` event. |
| 209 |
}, |
| 210 |
|
| 211 |
// Event when you move an item in the list or between lists |
| 212 |
onMove: function (/**Event*/evt, /**Event*/originalEvent) { |
| 213 |
// Example: https://jsbin.com/nawahef/edit?js,output |
| 214 |
evt.dragged; // dragged HTMLElement |
| 215 |
evt.draggedRect; // DOMRect {left, top, right, bottom} |
| 216 |
evt.related; // HTMLElement on which have guided |
| 217 |
evt.relatedRect; // DOMRect |
| 218 |
evt.willInsertAfter; // Boolean that is true if Sortable will insert drag element after target by default |
| 219 |
originalEvent.clientY; // mouse position |
| 220 |
// return false; — for cancel |
| 221 |
// return -1; — insert before target |
| 222 |
// return 1; — insert after target |
| 223 |
// return true; — keep default insertion point based on the direction |
| 224 |
// return void; — keep default insertion point based on the direction |
| 225 |
}, |
| 226 |
|
| 227 |
// Called when creating a clone of element |
| 228 |
onClone: function (/**Event*/evt) { |
| 229 |
var origEl = evt.item; |
| 230 |
var cloneEl = evt.clone; |
| 231 |
}, |
| 232 |
|
| 233 |
// Called when dragging element changes position |
| 234 |
onChange: function(/**Event*/evt) { |
| 235 |
evt.newIndex // most likely why this event is used is to get the dragging element's current index |
| 236 |
// same properties as onEnd |
| 237 |
} |
| 238 |
}); |
| 239 |
``` |
| 240 |
|
| 241 |
|
| 242 |
--- |
| 243 |
|
| 244 |
|
| 245 |
#### `group` option |
| 246 |
To drag elements from one list into another, both lists must have the same `group` value. |
| 247 |
You can also define whether lists can give away, give and keep a copy (`clone`), and receive elements. |
| 248 |
|
| 249 |
* name: `String` — group name |
| 250 |
* pull: `true|false|["foo", "bar"]|'clone'|function` — ability to move from the list. `clone` — copy the item, rather than move. Or an array of group names which the elements may be put in. Defaults to `true`. |
| 251 |
* put: `true|false|["baz", "qux"]|function` — whether elements can be added from other lists, or an array of group names from which elements can be added. |
| 252 |
* revertClone: `boolean` — revert cloned element to initial position after moving to a another list. |
| 253 |
|
| 254 |
|
| 255 |
Demo: |
| 256 |
- https://jsbin.com/hijetos/edit?js,output |
| 257 |
- https://jsbin.com/nacoyah/edit?js,output — use of complex logic in the `pull` and` put` |
| 258 |
- https://jsbin.com/bifuyab/edit?js,output — use `revertClone: true` |
| 259 |
|
| 260 |
|
| 261 |
--- |
| 262 |
|
| 263 |
|
| 264 |
#### `sort` option |
| 265 |
Allow sorting inside list. |
| 266 |
|
| 267 |
Demo: https://jsbin.com/jayedig/edit?js,output |
| 268 |
|
| 269 |
|
| 270 |
--- |
| 271 |
|
| 272 |
|
| 273 |
#### `delay` option |
| 274 |
Time in milliseconds to define when the sorting should start. |
| 275 |
Unfortunately, due to browser restrictions, delaying is not possible on IE or Edge with native drag & drop. |
| 276 |
|
| 277 |
Demo: https://jsbin.com/zosiwah/edit?js,output |
| 278 |
|
| 279 |
|
| 280 |
--- |
| 281 |
|
| 282 |
|
| 283 |
#### `delayOnTouchOnly` option |
| 284 |
Whether or not the delay should be applied only if the user is using touch (eg. on a mobile device). No delay will be applied in any other case. Defaults to `false`. |
| 285 |
|
| 286 |
|
| 287 |
--- |
| 288 |
|
| 289 |
|
| 290 |
#### `swapThreshold` option |
| 291 |
Percentage of the target that the swap zone will take up, as a float between `0` and `1`. |
| 292 |
|
| 293 |
[](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#swap-thresholdRead more](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#swap-threshold](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#swap-threshold) |
| 294 |
|
| 295 |
Demo: http://sortablejs.github.io/Sortable#thresholds |
| 296 |
|
| 297 |
|
| 298 |
--- |
| 299 |
|
| 300 |
|
| 301 |
#### `invertSwap` option |
| 302 |
Set to `true` to set the swap zone to the sides of the target, for the effect of sorting "in between" items. |
| 303 |
|
| 304 |
[](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#forcing-inverted-swap-zoneRead more](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#forcing-inverted-swap-zone](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#forcing-inverted-swap-zone) |
| 305 |
|
| 306 |
Demo: http://sortablejs.github.io/Sortable#thresholds |
| 307 |
|
| 308 |
|
| 309 |
--- |
| 310 |
|
| 311 |
|
| 312 |
#### `invertedSwapThreshold` option |
| 313 |
Percentage of the target that the inverted swap zone will take up, as a float between `0` and `1`. If not given, will default to `swapThreshold`. |
| 314 |
|
| 315 |
[](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#dealing-with-swap-glitchingRead more](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#dealing-with-swap-glitching](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#dealing-with-swap-glitching) |
| 316 |
|
| 317 |
|
| 318 |
--- |
| 319 |
|
| 320 |
|
| 321 |
#### `direction` option |
| 322 |
Direction that the Sortable should sort in. Can be set to `'vertical'`, `'horizontal'`, or a function, which will be called whenever a target is dragged over. Must return `'vertical'` or `'horizontal'`. |
| 323 |
|
| 324 |
[](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#directionRead more](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#direction](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#direction) |
| 325 |
|
| 326 |
|
| 327 |
Example of direction detection for vertical list that includes full column and half column elements: |
| 328 |
|
| 329 |
```js |
| 330 |
Sortable.create(el, { |
| 331 |
direction: function(evt, target, dragEl) { |
| 332 |
if (target !== null && target.className.includes('half-column') && dragEl.className.includes('half-column')) { |
| 333 |
return 'horizontal'; |
| 334 |
} |
| 335 |
return 'vertical'; |
| 336 |
} |
| 337 |
}); |
| 338 |
``` |
| 339 |
|
| 340 |
|
| 341 |
--- |
| 342 |
|
| 343 |
|
| 344 |
#### `touchStartThreshold` option |
| 345 |
This option is similar to `fallbackTolerance` option. |
| 346 |
|
| 347 |
When the `delay` option is set, some phones with very sensitive touch displays like the Samsung Galaxy S8 will fire |
| 348 |
unwanted touchmove events even when your finger is not moving, resulting in the sort not triggering. |
| 349 |
|
| 350 |
This option sets the minimum pointer movement that must occur before the delayed sorting is cancelled. |
| 351 |
|
| 352 |
Values between 3 to 5 are good. |
| 353 |
|
| 354 |
|
| 355 |
--- |
| 356 |
|
| 357 |
|
| 358 |
#### `disabled` options |
| 359 |
Disables the sortable if set to `true`. |
| 360 |
|
| 361 |
Demo: https://jsbin.com/sewokud/edit?js,output |
| 362 |
|
| 363 |
```js |
| 364 |
var sortable = Sortable.create(list); |
| 365 |
|
| 366 |
document.getElementById("switcher").onclick = function () { |
| 367 |
var state = sortable.option("disabled"); // get |
| 368 |
|
| 369 |
sortable.option("disabled", !state); // set |
| 370 |
}; |
| 371 |
``` |
| 372 |
|
| 373 |
|
| 374 |
--- |
| 375 |
|
| 376 |
|
| 377 |
#### `handle` option |
| 378 |
To make list items draggable, Sortable disables text selection by the user. |
| 379 |
That's not always desirable. To allow text selection, define a drag handler, |
| 380 |
which is an area of every list element that allows it to be dragged around. |
| 381 |
|
| 382 |
Demo: https://jsbin.com/numakuh/edit?html,js,output |
| 383 |
|
| 384 |
```js |
| 385 |
Sortable.create(el, { |
| 386 |
handle: ".my-handle" |
| 387 |
}); |
| 388 |
``` |
| 389 |
|
| 390 |
```html |
| 391 |
<ul> |
| 392 |
<li><span class="my-handle">::</span> list item text one |
| 393 |
<li><span class="my-handle">::</span> list item text two |
| 394 |
</ul> |
| 395 |
``` |
| 396 |
|
| 397 |
```css |
| 398 |
.my-handle { |
| 399 |
cursor: move; |
| 400 |
cursor: -webkit-grabbing; |
| 401 |
} |
| 402 |
``` |
| 403 |
|
| 404 |
|
| 405 |
--- |
| 406 |
|
| 407 |
|
| 408 |
#### `filter` option |
| 409 |
|
| 410 |
|
| 411 |
```js |
| 412 |
Sortable.create(list, { |
| 413 |
filter: ".js-remove, .js-edit", |
| 414 |
onFilter: function (evt) { |
| 415 |
var item = evt.item, |
| 416 |
ctrl = evt.target; |
| 417 |
|
| 418 |
if (Sortable.utils.is(ctrl, ".js-remove")) { // Click on remove button |
| 419 |
item.parentNode.removeChild(item); // remove sortable item |
| 420 |
} |
| 421 |
else if (Sortable.utils.is(ctrl, ".js-edit")) { // Click on edit link |
| 422 |
// ... |
| 423 |
} |
| 424 |
} |
| 425 |
}) |
| 426 |
``` |
| 427 |
|
| 428 |
|
| 429 |
--- |
| 430 |
|
| 431 |
|
| 432 |
#### `ghostClass` option |
| 433 |
Class name for the drop placeholder (default `sortable-ghost`). |
| 434 |
|
| 435 |
Demo: https://jsbin.com/henuyiw/edit?css,js,output |
| 436 |
|
| 437 |
```css |
| 438 |
.ghost { |
| 439 |
opacity: 0.4; |
| 440 |
} |
| 441 |
``` |
| 442 |
|
| 443 |
```js |
| 444 |
Sortable.create(list, { |
| 445 |
ghostClass: "ghost" |
| 446 |
}); |
| 447 |
``` |
| 448 |
|
| 449 |
|
| 450 |
--- |
| 451 |
|
| 452 |
|
| 453 |
#### `chosenClass` option |
| 454 |
Class name for the chosen item (default `sortable-chosen`). |
| 455 |
|
| 456 |
Demo: https://jsbin.com/hoqufox/edit?css,js,output |
| 457 |
|
| 458 |
```css |
| 459 |
.chosen { |
| 460 |
color: #fff; |
| 461 |
background-color: #c00; |
| 462 |
} |
| 463 |
``` |
| 464 |
|
| 465 |
```js |
| 466 |
Sortable.create(list, { |
| 467 |
delay: 500, |
| 468 |
chosenClass: "chosen" |
| 469 |
}); |
| 470 |
``` |
| 471 |
|
| 472 |
|
| 473 |
--- |
| 474 |
|
| 475 |
|
| 476 |
#### `forceFallback` option |
| 477 |
If set to `true`, the Fallback for non HTML5 Browser will be used, even if we are using an HTML5 Browser. |
| 478 |
This gives us the possibility to test the behaviour for older Browsers even in newer Browser, or make the Drag 'n Drop feel more consistent between Desktop , Mobile and old Browsers. |
| 479 |
|
| 480 |
On top of that, the Fallback always generates a copy of that DOM Element and appends the class `fallbackClass` defined in the options. This behaviour controls the look of this 'dragged' Element. |
| 481 |
|
| 482 |
Demo: https://jsbin.com/sibiput/edit?html,css,js,output |
| 483 |
|
| 484 |
|
| 485 |
--- |
| 486 |
|
| 487 |
|
| 488 |
#### `fallbackTolerance` option |
| 489 |
Emulates the native drag threshold. Specify in pixels how far the mouse should move before it's considered as a drag. |
| 490 |
Useful if the items are also clickable like in a list of links. |
| 491 |
|
| 492 |
When the user clicks inside a sortable element, it's not uncommon for your hand to move a little between the time you press and the time you release. |
| 493 |
Dragging only starts if you move the pointer past a certain tolerance, so that you don't accidentally start dragging every time you click. |
| 494 |
|
| 495 |
3 to 5 are probably good values. |
| 496 |
|
| 497 |
|
| 498 |
--- |
| 499 |
|
| 500 |
|
| 501 |
#### `dragoverBubble` option |
| 502 |
If set to `true`, the dragover event will bubble to parent sortables. Works on both fallback and native dragover event. |
| 503 |
By default, it is false, but Sortable will only stop bubbling the event once the element has been inserted into a parent Sortable, or *can* be inserted into a parent Sortable, but isn't at that specific time (due to animation, etc). |
| 504 |
|
| 505 |
Since 1.8.0, you will probably want to leave this option as false. Before 1.8.0, it may need to be `true` for nested sortables to work. |
| 506 |
|
| 507 |
|
| 508 |
--- |
| 509 |
|
| 510 |
|
| 511 |
#### `removeCloneOnHide` option |
| 512 |
If set to `false`, the clone is hidden by having it's CSS `display` property set to `none`. |
| 513 |
By default, this option is `true`, meaning Sortable will remove the cloned element from the DOM when it is supposed to be hidden. |
| 514 |
|
| 515 |
|
| 516 |
--- |
| 517 |
|
| 518 |
|
| 519 |
#### `emptyInsertThreshold` option |
| 520 |
The distance (in pixels) the mouse must be from an empty sortable while dragging for the drag element to be inserted into that sortable. Defaults to `5`. Set to `0` to disable this feature. |
| 521 |
|
| 522 |
Demo: https://jsbin.com/becavoj/edit?js,output |
| 523 |
|
| 524 |
An alternative to this option would be to set a padding on your list when it is empty. |
| 525 |
|
| 526 |
For example: |
| 527 |
```css |
| 528 |
ul:empty { |
| 529 |
padding-bottom: 20px; |
| 530 |
} |
| 531 |
``` |
| 532 |
|
| 533 |
Warning: For `:empty` to work, it must have no node inside (even text one). |
| 534 |
|
| 535 |
Demo: |
| 536 |
https://jsbin.com/yunakeg/edit?html,css,js,output |
| 537 |
|
| 538 |
--- |
| 539 |
### Event object ([demo](https://jsbin.com/fogujiv/edit?js,output)) |
| 540 |
|
| 541 |
- to:`HTMLElement` — list, in which moved element |
| 542 |
- from:`HTMLElement` — previous list |
| 543 |
- item:`HTMLElement` — dragged element |
| 544 |
- clone:`HTMLElement` |
| 545 |
- oldIndex:`Number|undefined` — old index within parent |
| 546 |
- newIndex:`Number|undefined` — new index within parent |
| 547 |
- oldDraggableIndex: `Number|undefined` — old index within parent, only counting draggable elements |
| 548 |
- newDraggableIndex: `Number|undefined` — new index within parent, only counting draggable elements |
| 549 |
- pullMode:`String|Boolean|undefined` — Pull mode if dragging into another sortable (`"clone"`, `true`, or `false`), otherwise undefined |
| 550 |
|
| 551 |
|
| 552 |
#### `move` event object |
| 553 |
- to:`HTMLElement` |
| 554 |
- from:`HTMLElement` |
| 555 |
- dragged:`HTMLElement` |
| 556 |
- draggedRect:`DOMRect` |
| 557 |
- related:`HTMLElement` — element on which have guided |
| 558 |
- relatedRect:`DOMRect` |
| 559 |
- willInsertAfter:`Boolean` — `true` if will element be inserted after target (or `false` if before) |
| 560 |
|
| 561 |
|
| 562 |
--- |
| 563 |
|
| 564 |
|
| 565 |
### Methods |
| 566 |
|
| 567 |
|
| 568 |
##### option(name:`String`[, value:`*`]):`*` |
| 569 |
Get or set the option. |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
##### closest(el:`HTMLElement`[, selector:`String`]):`HTMLElement|null` |
| 574 |
For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. |
| 575 |
|
| 576 |
|
| 577 |
##### toArray():`String[]` |
| 578 |
Serializes the sortable's item `data-id`'s (`dataIdAttr` option) into an array of string. |
| 579 |
|
| 580 |
|
| 581 |
##### sort(order:`String[]`, useAnimation:`Boolean`) |
| 582 |
Sorts the elements according to the array. |
| 583 |
|
| 584 |
```js |
| 585 |
var order = sortable.toArray(); |
| 586 |
sortable.sort(order.reverse(), true); // apply |
| 587 |
``` |
| 588 |
|
| 589 |
|
| 590 |
##### save() |
| 591 |
Save the current sorting (see [store](#store)) |
| 592 |
|
| 593 |
|
| 594 |
##### destroy() |
| 595 |
Removes the sortable functionality completely. |
| 596 |
|
| 597 |
|
| 598 |
--- |
| 599 |
|
| 600 |
|
| 601 |
<a name="store"></a> |
| 602 |
### Store |
| 603 |
Saving and restoring of the sort. |
| 604 |
|
| 605 |
```html |
| 606 |
<ul> |
| 607 |
<li data-id="1">order</li> |
| 608 |
<li data-id="2">save</li> |
| 609 |
<li data-id="3">restore</li> |
| 610 |
</ul> |
| 611 |
``` |
| 612 |
|
| 613 |
```js |
| 614 |
Sortable.create(el, { |
| 615 |
group: "localStorage-example", |
| 616 |
store: { |
| 617 |
/** |
| 618 |
* Get the order of elements. Called once during initialization. |
| 619 |
* @param {Sortable} sortable |
| 620 |
* @returns {Array} |
| 621 |
*/ |
| 622 |
get: function (sortable) { |
| 623 |
var order = localStorage.getItem(sortable.options.group.name); |
| 624 |
return order ? order.split('|') : []; |
| 625 |
}, |
| 626 |
|
| 627 |
/** |
| 628 |
* Save the order of elements. Called onEnd (when the item is dropped). |
| 629 |
* @param {Sortable} sortable |
| 630 |
*/ |
| 631 |
set: function (sortable) { |
| 632 |
var order = sortable.toArray(); |
| 633 |
localStorage.setItem(sortable.options.group.name, order.join('|')); |
| 634 |
} |
| 635 |
} |
| 636 |
}) |
| 637 |
``` |
| 638 |
|
| 639 |
|
| 640 |
--- |
| 641 |
|
| 642 |
|
| 643 |
<a name="bs"></a> |
| 644 |
### Bootstrap |
| 645 |
Demo: https://jsbin.com/visimub/edit?html,js,output |
| 646 |
|
| 647 |
```html |
| 648 |
<!-- Latest compiled and minified CSS --> |
| 649 |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"/> |
| 650 |
|
| 651 |
|
| 652 |
<!-- Latest Sortable --> |
| 653 |
<script src="http://SortableJS.github.io/Sortable/Sortable.js"></script> |
| 654 |
|
| 655 |
|
| 656 |
<!-- Simple List --> |
| 657 |
<ul id="simpleList" class="list-group"> |
| 658 |
<li class="list-group-item">This is <a href="http://SortableJS.github.io/Sortable/">Sortable</a></li> |
| 659 |
<li class="list-group-item">It works with Bootstrap...</li> |
| 660 |
<li class="list-group-item">...out of the box.</li> |
| 661 |
<li class="list-group-item">It has support for touch devices.</li> |
| 662 |
<li class="list-group-item">Just drag some elements around.</li> |
| 663 |
</ul> |
| 664 |
|
| 665 |
<script> |
| 666 |
// Simple list |
| 667 |
Sortable.create(simpleList, { /* options */ }); |
| 668 |
</script> |
| 669 |
``` |
| 670 |
|
| 671 |
|
| 672 |
--- |
| 673 |
|
| 674 |
|
| 675 |
### Static methods & properties |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
##### Sortable.create(el:`HTMLElement`[, options:`Object`]):`Sortable` |
| 680 |
Create new instance. |
| 681 |
|
| 682 |
|
| 683 |
--- |
| 684 |
|
| 685 |
|
| 686 |
##### Sortable.active:`Sortable` |
| 687 |
The active Sortable instance. |
| 688 |
|
| 689 |
|
| 690 |
--- |
| 691 |
|
| 692 |
|
| 693 |
##### Sortable.dragged:`HTMLElement` |
| 694 |
The element being dragged. |
| 695 |
|
| 696 |
|
| 697 |
--- |
| 698 |
|
| 699 |
|
| 700 |
##### Sortable.ghost:`HTMLElement` |
| 701 |
The ghost element. |
| 702 |
|
| 703 |
|
| 704 |
--- |
| 705 |
|
| 706 |
|
| 707 |
##### Sortable.clone:`HTMLElement` |
| 708 |
The clone element. |
| 709 |
|
| 710 |
|
| 711 |
--- |
| 712 |
|
| 713 |
|
| 714 |
##### Sortable.get(element:`HTMLElement`):`Sortable` |
| 715 |
Get the Sortable instance on an element. |
| 716 |
|
| 717 |
|
| 718 |
--- |
| 719 |
|
| 720 |
|
| 721 |
##### Sortable.mount(plugin:`...SortablePlugin|SortablePlugin[]`) |
| 722 |
Mounts a plugin to Sortable. |
| 723 |
|
| 724 |
|
| 725 |
--- |
| 726 |
|
| 727 |
|
| 728 |
##### Sortable.utils |
| 729 |
* on(el`:HTMLElement`, event`:String`, fn`:Function`) — attach an event handler function |
| 730 |
* off(el`:HTMLElement`, event`:String`, fn`:Function`) — remove an event handler |
| 731 |
* css(el`:HTMLElement`)`:Object` — get the values of all the CSS properties |
| 732 |
* css(el`:HTMLElement`, prop`:String`)`:Mixed` — get the value of style properties |
| 733 |
* css(el`:HTMLElement`, prop`:String`, value`:String`) — set one CSS properties |
| 734 |
* css(el`:HTMLElement`, props`:Object`) — set more CSS properties |
| 735 |
* find(ctx`:HTMLElement`, tagName`:String`[, iterator`:Function`])`:Array` — get elements by tag name |
| 736 |
* bind(ctx`:Mixed`, fn`:Function`)`:Function` — Takes a function and returns a new one that will always have a particular context |
| 737 |
* is(el`:HTMLElement`, selector`:String`)`:Boolean` — check the current matched set of elements against a selector |
| 738 |
* closest(el`:HTMLElement`, selector`:String`[, ctx`:HTMLElement`])`:HTMLElement|Null` — for each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree |
| 739 |
* clone(el`:HTMLElement`)`:HTMLElement` — create a deep copy of the set of matched elements |
| 740 |
* toggleClass(el`:HTMLElement`, name`:String`, state`:Boolean`) — add or remove one classes from each element |
| 741 |
* detectDirection(el`:HTMLElement`)`:String` — automatically detect the [](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#directiondirection](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#direction](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#direction) of the element as either `'vertical'` or `'horizontal'` |
| 742 |
* index(el`:HTMLElement`, selector`:String`)`:Number` — index of the element within its parent for a selected set of elements |
| 743 |
* getChild(el`:HTMLElement`, childNum`:Number`, options`:Object`, includeDragEl`:Boolean`):`HTMLElement` — get the draggable element at a given index of draggable elements within a Sortable instance |
| 744 |
* expando`:String` — expando property name for internal use, sortableListElement[expando] returns the Sortable instance of that elemenet |
| 745 |
--- |
| 746 |
|
| 747 |
|
| 748 |
### Plugins |
| 749 |
#### Extra Plugins (included in complete versions) |
| 750 |
- [](https://github.com/SortableJS/Sortable/tree/master/plugins/MultiDragMultiDrag](https://github.com/SortableJS/Sortable/tree/master/plugins/MultiDrag](https://github.com/SortableJS/Sortable/tree/master/plugins/MultiDrag) |
| 751 |
- [](https://github.com/SortableJS/Sortable/tree/master/plugins/SwapSwap](https://github.com/SortableJS/Sortable/tree/master/plugins/Swap](https://github.com/SortableJS/Sortable/tree/master/plugins/Swap) |
| 752 |
|
| 753 |
#### Default Plugins (included in default versions) |
| 754 |
- [](https://github.com/SortableJS/Sortable/tree/master/plugins/AutoScrollAutoScroll](https://github.com/SortableJS/Sortable/tree/master/plugins/AutoScroll](https://github.com/SortableJS/Sortable/tree/master/plugins/AutoScroll) |
| 755 |
- [](https://github.com/SortableJS/Sortable/tree/master/plugins/OnSpillOnSpill](https://github.com/SortableJS/Sortable/tree/master/plugins/OnSpill](https://github.com/SortableJS/Sortable/tree/master/plugins/OnSpill) |
| 756 |
|
| 757 |
|
| 758 |
--- |
| 759 |
|
| 760 |
|
| 761 |
<a name="cdn"></a> |
| 762 |
### CDN |
| 763 |
|
| 764 |
```html |
| 765 |
<!-- jsDelivr :: Sortable :: Latest (https://www.jsdelivr.com/package/npm/sortablejs) --> |
| 766 |
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script> |
| 767 |
``` |
| 768 |
|
| 769 |
|
| 770 |
--- |
| 771 |
|
| 772 |
|
| 773 |
### Contributing (Issue/PR) |
| 774 |
|
| 775 |
Please, [](CONTRIBUTING.mdread this](CONTRIBUTING.md](CONTRIBUTING.md). |
| 776 |
|
| 777 |
|
| 778 |
--- |
| 779 |
|
| 780 |
|
| 781 |
## Contributors |
| 782 |
|
| 783 |
### Code Contributors |
| 784 |
|
| 785 |
This project exists thanks to all the people who contribute. [[](CONTRIBUTING.mdContribute](CONTRIBUTING.md](CONTRIBUTING.md)]. |
| 786 |
<a href="https://github.com/SortableJS/Sortable/graphs/contributors"><img src="https://opencollective.com/Sortable/contributors.svg?width=890&button=false" /></a> |
| 787 |
|
| 788 |
### Financial Contributors |
| 789 |
|
| 790 |
Become a financial contributor and help us sustain our community. [[](https://opencollective.com/Sortable/contributeContribute](https://opencollective.com/Sortable/contribute](https://opencollective.com/Sortable/contribute)] |
| 791 |
|
| 792 |
#### Individuals |
| 793 |
|
| 794 |
<a href="https://opencollective.com/Sortable"><img src="https://opencollective.com/Sortable/individuals.svg?width=890"></a> |
| 795 |
|
| 796 |
#### Organizations |
| 797 |
|
| 798 |
Support this project with your organization. Your logo will show up here with a link to your website. [[](https://opencollective.com/Sortable/contributeContribute](https://opencollective.com/Sortable/contribute](https://opencollective.com/Sortable/contribute)] |
| 799 |
|
| 800 |
<a href="https://opencollective.com/Sortable/organization/0/website"><img src="https://opencollective.com/Sortable/organization/0/avatar.svg"></a> |
| 801 |
<a href="https://opencollective.com/Sortable/organization/1/website"><img src="https://opencollective.com/Sortable/organization/1/avatar.svg"></a> |
| 802 |
<a href="https://opencollective.com/Sortable/organization/2/website"><img src="https://opencollective.com/Sortable/organization/2/avatar.svg"></a> |
| 803 |
<a href="https://opencollective.com/Sortable/organization/3/website"><img src="https://opencollective.com/Sortable/organization/3/avatar.svg"></a> |
| 804 |
<a href="https://opencollective.com/Sortable/organization/4/website"><img src="https://opencollective.com/Sortable/organization/4/avatar.svg"></a> |
| 805 |
<a href="https://opencollective.com/Sortable/organization/5/website"><img src="https://opencollective.com/Sortable/organization/5/avatar.svg"></a> |
| 806 |
<a href="https://opencollective.com/Sortable/organization/6/website"><img src="https://opencollective.com/Sortable/organization/6/avatar.svg"></a> |
| 807 |
<a href="https://opencollective.com/Sortable/organization/7/website"><img src="https://opencollective.com/Sortable/organization/7/avatar.svg"></a> |
| 808 |
<a href="https://opencollective.com/Sortable/organization/8/website"><img src="https://opencollective.com/Sortable/organization/8/avatar.svg"></a> |
| 809 |
<a href="https://opencollective.com/Sortable/organization/9/website"><img src="https://opencollective.com/Sortable/organization/9/avatar.svg"></a> |
| 810 |
|
| 811 |
## MIT LICENSE |
| 812 |
Permission is hereby granted, free of charge, to any person obtaining |
| 813 |
a copy of this software and associated documentation files (the |
| 814 |
"Software"), to deal in the Software without restriction, including |
| 815 |
without limitation the rights to use, copy, modify, merge, publish, |
| 816 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 817 |
permit persons to whom the Software is furnished to do so, subject to |
| 818 |
the following conditions: |
| 819 |
|
| 820 |
The above copyright notice and this permission notice shall be |
| 821 |
included in all copies or substantial portions of the Software. |
| 822 |
|
| 823 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 824 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 825 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 826 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 827 |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 828 |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 829 |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 830 |
|