sortable

功能描述

  • 该模块实现移动端拖动排序功能
  • 可以从一个列表拖动到另一个列表或在同一列表中
  • 移动项目时的CSS动画
  • 智能自动滚动
  • 使用原生HTML5拖放API构建
  • 简单的API
  • 不依赖于任何前端框架

依赖的模块

快速使用

参数示例

  1. var sortable = new Sortable(el, {
  2. group: "name", // or { name: "...", pull: [true, false, 'clone', array], put: [true, false, array] }
  3. sort: true, // sorting inside list
  4. delay: 0, // time in milliseconds to define when the sorting should start
  5. delayOnTouchOnly: false, // only delay if user is using touch
  6. touchStartThreshold: 0, // px, how many pixels the point should move before cancelling a delayed drag event
  7. disabled: false, // Disables the sortable if set to true.
  8. store: null, // @see Store
  9. animation: 150, // ms, animation speed moving items when sorting, `0` — without animation
  10. easing: "cubic-bezier(1, 0, 0, 1)", // Easing for animation. Defaults to null. See https://easings.net/ for examples.
  11. handle: ".my-handle", // Drag handle selector within list items
  12. filter: ".ignore-elements", // Selectors that do not lead to dragging (String or Function)
  13. preventOnFilter: true, // Call `event.preventDefault()` when triggered `filter`
  14. draggable: ".item", // Specifies which items inside the element should be draggable
  15. ghostClass: "sortable-ghost", // Class name for the drop placeholder
  16. chosenClass: "sortable-chosen", // Class name for the chosen item
  17. dragClass: "sortable-drag", // Class name for the dragging item
  18. dataIdAttr: 'data-id',
  19. swapThreshold: 1, // Threshold of the swap zone
  20. invertSwap: false, // Will always use inverted swap zone if set to true
  21. invertedSwapThreshold: 1, // Threshold of the inverted swap zone (will be set to swapThreshold value by default)
  22. direction: 'horizontal', // Direction of Sortable (will be detected automatically if not given)
  23. forceFallback: false, // ignore the HTML5 DnD behaviour and force the fallback to kick in
  24. fallbackClass: "sortable-fallback", // Class name for the cloned DOM Element when using forceFallback
  25. fallbackOnBody: false, // Appends the cloned DOM Element into the Document's Body
  26. fallbackTolerance: 0, // Specify in pixels how far the mouse should move before it's considered as a drag.
  27. scroll: true, // or HTMLElement
  28. scrollFn: function(offsetX, offsetY, originalEvent, touchEvt, hoverTargetEl) { ... }, // if you have custom scrollbar scrollFn may be used for autoscrolling
  29. scrollSpeed: 10, // px
  30. bubbleScroll: true, // apply autoscroll to all parent elements, allowing for easier movement
  31. dragoverBubble: false,
  32. removeCloneOnHide: true, // Remove the clone element when it is not showing, rather than just hiding it
  33. emptyInsertThreshold: 5, // px, distance mouse must be from empty sortable to insert drag element into it
  34. setData: function (/** DataTransfer */dataTransfer, /** HTMLElement*/dragEl) {
  35. dataTransfer.setData('Text', dragEl.textContent); // `dataTransfer` object of HTML5 DragEvent
  36. },
  37. // Element is chosen
  38. onChoose: function (/**Event*/evt) {
  39. evt.oldIndex; // element index within parent
  40. },
  41. // Element is unchosen
  42. onUnchoose: function(/**Event*/evt) {
  43. // same properties as onEnd
  44. },
  45. // Element dragging started
  46. onStart: function (/**Event*/evt) {
  47. evt.oldIndex; // element index within parent
  48. },
  49. // Element dragging ended
  50. onEnd: function (/**Event*/evt) {
  51. var itemEl = evt.item; // dragged HTMLElement
  52. evt.to; // target list
  53. evt.from; // previous list
  54. evt.oldIndex; // element's old index within old parent
  55. evt.newIndex; // element's new index within new parent
  56. evt.oldDraggableIndex; // element's old index within old parent, only counting draggable elements
  57. evt.newDraggableIndex; // element's new index within new parent, only counting draggable elements
  58. evt.clone // the clone element
  59. evt.pullMode; // when item is in another sortable: `"clone"` if cloning, `true` if moving
  60. },
  61. // Element is dropped into the list from another list
  62. onAdd: function (/**Event*/evt) {
  63. // same properties as onEnd
  64. },
  65. onUpdate: function (/**Event*/evt) {
  66. // same properties as onEnd
  67. // Called by any change to the list (add / update / remove)
  68. onSort: function (/**Event*/evt) {
  69. // same properties as onEnd
  70. },
  71. // Element is removed from the list into another list
  72. onRemove: function (/**Event*/evt) {
  73. // same properties as onEnd
  74. },
  75. // Attempt to drag a filtered element
  76. onFilter: function (/**Event*/evt) {
  77. var itemEl = evt.item; // HTMLElement receiving the `mousedown|tapstart` event.
  78. },
  79. // Event when you move an item in the list or between lists
  80. onMove: function (/**Event*/evt, /**Event*/originalEvent) {
  81. // Example: https://jsbin.com/nawahef/edit?js,output
  82. evt.dragged; // dragged HTMLElement
  83. evt.draggedRect; // DOMRect {left, top, right, bottom}
  84. evt.related; // HTMLElement on which have guided
  85. evt.relatedRect; // DOMRect
  86. evt.willInsertAfter; // Boolean that is true if Sortable will insert drag element after target by default
  87. originalEvent.clientY; // mouse position
  88. // return false; — for cancel
  89. // return -1; — insert before target
  90. // return 1; — insert after target
  91. },
  92. // Called when creating a clone of element
  93. onClone: function (/**Event*/evt) {
  94. var origEl = evt.item;
  95. var cloneEl = evt.clone;
  96. },
  97. // Called when dragging element changes position
  98. onChange: function(/**Event*/evt) {
  99. evt.newIndex // most likely why this event is used is to get the dragging element's current index
  100. // same properties as onEnd
  101. }

特别说明