Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required.
Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required.
Sortable is a JavaScript library for reorderable drag-and-drop lists.
Demo: http://sortablejs.github.io/Sortable/
@types/sortablejsInstall with NPM:
npm install sortablejs --saveInstall with Bower:
bower install --save sortablejsImport into your project:
// Default SortableJS
import Sortable from 'sortablejs';
// Core SortableJS (without default plugins)
import Sortable from 'sortablejs/modular/sortable.core.esm.js';
// Complete SortableJS (with all plugins)
import Sortable from 'sortablejs/modular/sortable.complete.esm.js';Cherrypick plugins:
// Cherrypick extra plugins
import Sortable, { MultiDrag, Swap } from 'sortablejs';
Sortable.mount(new MultiDrag(), new Swap());
// Cherrypick default plugins
import Sortable, { AutoScroll } from 'sortablejs/modular/sortable.core.esm.js';
Sortable.mount(new AutoScroll());<ul id="items">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>var el = document.getElementById('items');
var sortable = Sortable.create(el);You can use any element for the list and its elements, not just ul/li. Here is an example with divs.
…group optionTo drag elements from one list into another, both lists must have the same group value.
You can also define whether lists can give away, give and keep a copy (clone), and receive elements.
String — group nametrue|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.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.boolean — revert cloned element to initial position after moving to a another list.Demo:
pull and putrevertClone: truesort optionAllow sorting inside list.
Demo: https://jsbin.com/jayedig/edit?js,output
delay optionTime in milliseconds to define when the sorting should start. Unfortunately, due to browser restrictions, delaying is not possible on IE or Edge with native drag & drop.
Demo: https://jsbin.com/zosiwah/edit?js,output
delayOnTouchOnly optionWhether 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.
swapThreshold optionPercentage of the target that the swap zone will take up, as a float between 0 and 1.
Demo: http://sortablejs.github.io/Sortable#thresholds
invertSwap optionSet to true to set the swap zone to the sides of the target, for the effect of sorting "in between" items.
Demo: http://sortablejs.github.io/Sortable#thresholds
invertedSwapThreshold optionPercentage 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.
direction optionDirection 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'.
Example of direction detection for vertical list that includes full column and half column elements:
Sortable.create(el, {
direction: function(evt, target, dragEl) {
if (target !== null && target.className.includes('half-column') && dragEl.className.includes('half-column')) {
return 'horizontal';
}
return 'vertical';
}
});touchStartThreshold optionThis option is similar to fallbackTolerance option.
When the delay option is set, some phones with very sensitive touch displays like the Samsung Galaxy S8 will fire
unwanted touchmove events even when your finger is not moving, resulting in the sort not triggering.
This option sets the minimum pointer movement that must occur before the delayed sorting is cancelled.
Values between 3 to 5 are good.
disabled optionsDisables the sortable if set to true.
Demo: https://jsbin.com/sewokud/edit?js,output
var sortable = Sortable.create(list);
document.getElementById("switcher").onclick = function () {
var state = sortable.option("disabled"); // get
sortable.option("disabled", !state); // set
};handle optionTo make list items draggable, Sortable disables text selection by the user. That's not always desirable. To allow text selection, define a drag handler, which is an area of every list element that allows it to be dragged around.
Demo: https://jsbin.com/numakuh/edit?html,js,output
Sortable.create(el, {
handle: ".my-handle"
});<ul>
<li><span class="my-handle">::</span> list item text one
<li><span class="my-handle">::</span> list item text two
</ul>.my-handle {
cursor: move;
cursor: -webkit-grabbing;
}filter optionSortable.create(list, {
filter: ".js-remove, .js-edit",
onFilter: function (evt) {
var item = evt.item,
ctrl = evt.target;
if (Sortable.utils.is(ctrl, ".js-remove")) { // Click on remove button
item.parentNode.removeChild(item); // remove sortable item
}
else if (Sortable.utils.is(ctrl, ".js-edit")) { // Click on edit link
// ...
}
}
})ghostClass optionClass name for the drop placeholder (default sortable-ghost).
Demo: https://jsbin.com/henuyiw/edit?css,js,output
.ghost {
opacity: 0.4;
}Sortable.create(list, {
ghostClass: "ghost"
});chosenClass optionClass name for the chosen item (default sortable-chosen).
Demo: https://jsbin.com/hoqufox/edit?css,js,output
.chosen {
color: #fff;
background-color: #c00;
}Sortable.create(list, {
delay: 500,
chosenClass: "chosen"
});forceFallback optionIf set to true, the Fallback for non HTML5 Browser will be used, even if we are using an HTML5 Browser.
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.
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.
Demo: https://jsbin.com/sibiput/edit?html,css,js,output
fallbackTolerance optionEmulates the native drag threshold. Specify in pixels how far the mouse should move before it's considered as a drag. Useful if the items are also clickable like in a list of links.
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. Dragging only starts if you move the pointer past a certain tolerance, so that you don't accidentally start dragging every time you click.
3 to 5 are probably good values.
dragoverBubble optionIf set to true, the dragover event will bubble to parent sortables. Works on both fallback and native dragover event.
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).
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.
removeCloneOnHide optionIf set to false, the clone is hidden by having it's CSS display property set to none.
By default, this option is true, meaning Sortable will remove the cloned element from the DOM when it is supposed to be hidden.
emptyInsertThreshold optionThe 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.
Demo: https://jsbin.com/becavoj/edit?js,output
An alternative to this option would be to set a padding on your list when it is empty.
For example:
ul:empty {
padding-bottom: 20px;
}Warning: For :empty to work, it must have no node inside (even text one).
Demo: https://jsbin.com/yunakeg/edit?html,css,js,output
HTMLElement — list, in which moved elementHTMLElement — previous listHTMLElement — dragged elementHTMLElementNumber|undefined — old index within parentNumber|undefined — new index within parentNumber|undefined — old index within parent, only counting draggable elementsNumber|undefined — new index within parent, only counting draggable elementsString|Boolean|undefined — Pull mode if dragging intModern CI Pipeline: Playwright + GitHub Actions (replaces TestCafe/Sauce Labs)
[bug] Erratic ghost element position animation during drag with substantial amount of elements
vue中动态渲染filter状态为什么不会更新class?
Multple classes aren't allowed in chosenClass
[bug] Destroy Sortable not remove all data from elements.
Safari Zoom Issue
[feature] Evokable Drag method
[bug] Fallback doesn't move on iOS when inside shadow root
[bug] Uncaught TypeError: Cannot read properties of null
Aurelia 2.0 RC: Custom drag containers implemented for SortableJS!