Baike.dev
All toolsTrendingOpen sourceNewsSubmit
Log in
< 返回工具列表
S

Sortable

> 编程语言
开源

Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required.

31.2K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required.

# Sortable   [](https://opencollective.com/Sortable) [](https://circleci.com/gh/SortableJS/Sortable) [](https://deepscan.io/dashboard#view=project&tid=3901&pid=5666&bid=43977) [](https://www.jsdelivr.com/package/npm/sortablejs) [](https://www.npmjs.com/package/sortablejs) Sortable is a JavaScript library for reorderable drag-and-drop lists. Demo: http://sortablejs.github.io/Sortable/ [](https://saucelabs.com/) ## Features * Supports touch devices and [modern](http://caniuse.com/#search=drag) browsers (including IE9) * Can drag from one list to another or within the same list * CSS animation when moving items * Supports drag handles *and selectable text* (better than voidberg's html5sortable) * Smart auto-scrolling * Advanced swap detection * Smooth animations * [Multi-drag](https://github.com/SortableJS/Sortable/tree/master/plugins/MultiDrag) support * Support for CSS transforms * Built using native HTML5 drag and drop API * Supports * [Meteor](https://github.com/SortableJS/meteor-sortablejs) * Angular * [2.0+](https://github.com/SortableJS/angular-sortablejs) * [1.*](https://github.com/SortableJS/angular-legacy-sortablejs) * React * [ES2015+](https://github.com/SortableJS/react-sortablejs) * [Mixin](https://github.com/SortableJS/react-mixin-sortablejs) * [Knockout](https://github.com/SortableJS/knockout-sortablejs) * [Polymer](https://github.com/SortableJS/polymer-sortablejs) * [Vue](https://github.com/SortableJS/Vue.Draggable) * [Ember](https://github.com/SortableJS/ember-sortablejs) * Supports any CSS library, e.g. [Bootstrap](#bs) * Simple API * Support for [plugins](#plugins) * [CDN](#cdn) * No jQuery required (but there is [support](https://github.com/SortableJS/jquery-sortablejs)) * Typescript definitions at `@types/sortablejs`
### Articles * [Dragging Multiple Items in Sortable](https://github.com/SortableJS/Sortable/wiki/Dragging-Multiple-Items-in-Sortable) (April 26, 2019) * [Swap Thresholds and Direction](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction) (December 2, 2018) * [Sortable v1.0 — New capabilities](https://github.com/SortableJS/Sortable/wiki/Sortable-v1.0-—-New-capabilities/) (December 22, 2014) * [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)
### Getting Started Install with NPM: ```bash npm install sortablejs --save ``` Install with Bower: ```bash bower install --save sortablejs ``` Import into your project: ```js // 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: ```js // 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()); ``` --- ### Usage ```html
  • item 1
  • item 2
  • item 3
``` ```js 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 `div`s](https://jsbin.com/visimub/edit?html,js,output). --- ### Options ``` … ``` --- #### `group` option To 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. * name: `String` — group name * 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`. * 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. * revertClone: `boolean` — revert cloned element to initial position after moving to a another list. Demo: - https://jsbin.com/hijetos/edit?js,output - https://jsbin.com/nacoyah/edit?js,output — use of complex logic in the `pull` and` put` - https://jsbin.com/bifuyab/edit?js,output — use `revertClone: true` --- #### `sort` option Allow sorting inside list. Demo: https://jsbin.com/jayedig/edit?js,output --- #### `delay` option Time 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` option 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`. --- #### `swapThreshold` option Percentage of the target that the swap zone will take up, as a float between `0` and `1`. [Read more](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#swap-threshold) Demo: http://sortablejs.github.io/Sortable#thresholds --- #### `invertSwap` option Set to `true` to set the swap zone to the sides of the target, for the effect of sorting "in between" items. [Read more](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#forcing-inverted-swap-zone) Demo: http://sortablejs.github.io/Sortable#thresholds --- #### `invertedSwapThreshold` option 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`. [Read more](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#dealing-with-swap-glitching) --- #### `direction` option 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'`. [Read more](https://github.com/SortableJS/Sortable/wiki/Swap-Thresholds-and-Direction#direction) Example of direction detection for vertical list that includes full column and half column elements: ```js 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` option This 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` options Disables the sortable if set to `true`. Demo: https://jsbin.com/sewokud/edit?js,output ```js var sortable = Sortable.create(list); document.getElementById("switcher").onclick = function () { var state = sortable.option("disabled"); // get sortable.option("disabled", !state); // set }; ``` --- #### `handle` option To 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 ```js Sortable.create(el, { handle: ".my-handle" }); ``` ```html
  • :: list item text one
  • :: list item text two
``` ```css .my-handle { cursor: move; cursor: -webkit-grabbing; } ``` --- #### `filter` option ```js Sortable.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` option Class name for the drop placeholder (default `sortable-ghost`). Demo: https://jsbin.com/henuyiw/edit?css,js,output ```css .ghost { opacity: 0.4; } ``` ```js Sortable.create(list, { ghostClass: "ghost" }); ``` --- #### `chosenClass` option Class name for the chosen item (default `sortable-chosen`). Demo: https://jsbin.com/hoqufox/edit?css,js,output ```css .chosen { color: #fff; background-color: #c00; } ``` ```js Sortable.create(list, { delay: 500, chosenClass: "chosen" }); ``` --- #### `forceFallback` option If 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` option Emulates 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` option If 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` option If 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` option 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. 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: ```css 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 --- ### Event object ([demo](https://jsbin.com/fogujiv/edit?js,output)) - to:`HTMLElement` — list, in which moved element - from:`HTMLElement` — previous list - item:`HTMLElement` — dragged element - clone:`HTMLElement` - oldIndex:`Number|undefined` — old index within parent - newIndex:`Number|undefined` — new index within parent - oldDraggableIndex: `Number|undefined` — old index within parent, only counting draggable elements - newDraggableIndex: `Number|undefined` — new index within parent, only counting draggable elements - pullMode:`String|Boolean|undefined` — Pull mode if dragging int

核心特点

  • •Supports touch devices and modern browsers (including IE9)
  • •Can drag from one list to another or within the same list
  • •CSS animation when moving items
  • •Supports drag handles *and selectable text* (better than voidberg's html5sortable)
  • •Smart auto-scrolling
  • •Advanced swap detection
  • •Smooth animations
  • •Multi-drag support
  • •Support for CSS transforms
  • •Built using native HTML5 drag and drop API

> 标签

JavaScriptdragdrag-and-dropdrag-dropdraggable

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月9日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言