經測試,Sortable.js 兼容好和使用方便都是比較不錯的,特別手機端使用很棒
介紹
Sortable.js是一款輕量級的拖放排序列表的js插件(雖然體積小,但是功能很強大)
下載地址:https://github.com/RubaXa/Sor...
官方DEMO:https://sortablejs.github.io/Sortable
特點
-
支持觸屏設備和大部分瀏覽器(IE9以下的就不支持了,原因都懂得)
-
可以從一個列表容器中拖拽一個列表單元到其他容器或本列表容器中進行排序
-
移動列表單元時有css動畫
-
支持拖放操作和可選擇的文本(這句我也沒理解,大概意思就是對原生的拖放進行拓展了)
-
非常友善的滾動效果
-
基於原生HTML5中的拖放API
-
支持多種框架(angular、vue、react等)
-
支持所有的css框架,像Bootstrap
-
簡單的API,方便使用
-
CDN
-
不依賴jQuery
VUE框架中使用,見這里:http://www.cnblogs.com/xiangsj/p/7278663.html
使用舉例
<script src="../js/sortable.min.js?v=0.0"></script>
var el = document.getElementById('items'); new Sortable(el);
//常用 new Sortable(el, { handle: ".my-handle", // 拖拽區域,默認為 items 的 子元素 onStart: function (/**Event*/evt) { // 拖拽開始 var itemEl = evt.item;// 當前拖拽的html元素 }, onEnd: function (/**Event*/evt) { // 拖拽結束 var itemEl = evt.item; } });
如是2組之間拖放:
new Sortable(document.getElementById('C_fieldSet_01'), { group: ".C_move", handle: ".C_move" }); new Sortable(document.getElementById('C_fieldSet_02'), { group: ".C_move", handle: ".C_move" });
配置項
var sortable = new Sortable(el, { group: "name", // or { name: "...", pull: [true, false, clone], put: [true, false, array] } sort: true, // sorting inside list delay: 0, // time in milliseconds to define when the sorting should start disabled: false, // Disables the sortable if set to true. store: null, // @see Store animation: 150, // ms, animation speed moving items when sorting, `0` — without animation handle: ".my-handle", // Drag handle selector within list items filter: ".ignore-elements", // Selectors that do not lead to dragging (String or Function) draggable: ".item", // Specifies which items inside the element should be draggable ghostClass: "sortable-ghost", // Class name for the drop placeholder chosenClass: "sortable-chosen", // Class name for the chosen item dragClass: "sortable-drag", // Class name for the dragging item dataIdAttr: 'data-id', forceFallback: false, // ignore the HTML5 DnD behaviour and force the fallback to kick in fallbackClass: "sortable-fallback", // Class name for the cloned DOM Element when using forceFallback fallbackOnBody: false, // Appends the cloned DOM Element into the Document's Body fallbackTolerance: 0, // Specify in pixels how far the mouse should move before it's considered as a drag. scroll: true, // or HTMLElement scrollFn: function(offsetX, offsetY, originalEvent) { ... }, // if you have custom scrollbar scrollFn may be used for autoscrolling scrollSensitivity: 30, // px, how near the mouse must be to an edge to start scrolling. scrollSpeed: 10, // px setData: function (/** DataTransfer */dataTransfer, /** HTMLElement*/dragEl) { dataTransfer.setData('Text', dragEl.textContent); // `dataTransfer` object of HTML5 DragEvent }, // Element is chosen onChoose: function (/**Event*/evt) { evt.oldIndex; // element index within parent }, // Element dragging started onStart: function (/**Event*/evt) { evt.oldIndex; // element index within parent }, // Element dragging ended onEnd: function (/**Event*/evt) { evt.oldIndex; // element's old index within parent evt.newIndex; // element's new index within parent }, // Element is dropped into the list from another list onAdd: function (/**Event*/evt) { var itemEl = evt.item; // dragged HTMLElement evt.from; // previous list // + indexes from onEnd }, // Changed sorting within list onUpdate: function (/**Event*/evt) { var itemEl = evt.item; // dragged HTMLElement // + indexes from onEnd }, // Called by any change to the list (add / update / remove) onSort: function (/**Event*/evt) { // same properties as onUpdate }, // Element is removed from the list into another list onRemove: function (/**Event*/evt) { // same properties as onUpdate }, // Attempt to drag a filtered element onFilter: function (/**Event*/evt) { var itemEl = evt.item; // HTMLElement receiving the `mousedown|tapstart` event. }, // Event when you move an item in the list or between lists onMove: function (/**Event*/evt, /**Event*/originalEvent) { // Example: http://jsbin.com/tuyafe/1/edit?js,output evt.dragged; // dragged HTMLElement evt.draggedRect; // TextRectangle {left, top, right и bottom} evt.related; // HTMLElement on which have guided evt.relatedRect; // TextRectangle originalEvent.clientY; // mouse position // return false; — for cancel }, // Called when creating a clone of element onClone: function (/**Event*/evt) { var origEl = evt.item; var cloneEl = evt.clone; } });
屬性
-
group:string or array
-
sort:boolean 定義是否列表單元是否可以在列表容器內進行拖拽排序;
-
delay:number 定義鼠標選中列表單元可以開始拖動的延遲時間;
-
disabled:boolean 定義是否此sortable對象是否可用,為true時sortable對象不能拖放排序等功能,為false時為可以進行排序,相當於一個開關;
-
animation:number 單位:ms,定義排序動畫的時間;
-
handle:selector 格式為簡單css選擇器的字符串,使列表單元中符合選擇器的元素成為拖動的手柄,只有按住拖動手柄才能使列表單元進行拖動;
-
filter:selector 格式為簡單css選擇器的字符串,定義哪些列表單元不能進行拖放,可設置為多個選擇器,中間用“,”分隔;
-
draggable:selector 格式為簡單css選擇器的字符串,定義哪些列表單元可以進行拖放
-
ghostClass:selector 格式為簡單css選擇器的字符串,當拖動列表單元時會生成一個副本作為影子單元來模擬被拖動單元排序的情況,此配置項就是來給這個影子單元添加一個class,我們可以通過這種方式來給影子元素進行編輯樣式;
-
chosenClass:selector 格式為簡單css選擇器的字符串,當選中列表單元時會給該單元增加一個class;
-
forceFallback:boolean 如果設置為true時,將不使用原生的html5的拖放,可以修改一些拖放中元素的樣式等;
-
fallbackClass:string 當forceFallback設置為true時,拖放過程中鼠標附着單元的樣式;
-
scroll:boolean 默認為true,當排序的容器是個可滾動的區域,拖放可以引起區域滾動
事件: -
onChoose:function 列表單元被選中的回調函數
-
onStart:function 列表單元拖動開始的回調函數
-
onEnd:function 列表單元拖放結束后的回調函數
-
onAdd:function 列表單元添加到本列表容器的回調函數
-
onUpdate:function 列表單元在列表容器中的排序發生變化后的回調函數
-
onRemove:function 列表元素移到另一個列表容器的回調函數
-
onFilter:function 試圖選中一個被filter過濾的列表單元的回調函數
-
onMove:function 當移動列表單元在一個列表容器中或者多個列表容器中的回調函數
-
onClone:function 當創建一個列表單元副本的時候的回調函數
事件對象:
事件對象在各個函數中略有不同,可通過輸出對象查看對象的屬性,下面簡單列舉幾個:
-
to:HTMLElement--移動到列表容器
-
from:HTMLElement--來源的列表容器
-
item:HTMLElement--被移動的列表單元
-
clone:HTMLElement--副本的列表單元
-
oldIndex:number/undefined--在列表容器中的原序號
-
newIndex:number/undefined--在列表容器中的新序號
方法
-
option(name[,value])
獲得或者設置項參數,使用方法類似於jQuery用法,沒有第二個參數為獲得option中第一個參數所對應的值,有第二個參數時,將重新賦給第一個參數所對應的值; -
closest
沒理解 -
toArray()
序列化可排序的列表單元的data-id(可通過配置項中dataIdAttr修改)放入一個數組,並返回這個數組中 -
sort()
通過自定義列表單元的data-id的數組對列表單元進行排序 -
save()
-
destroy()
收集來源:
https://segmentfault.com/a/1190000008209715 (介紹的比較全面)
http://www.jqcool.net/sortable.html
.