Sortable.js是一款輕量級的拖放排序列表的js插件(雖然體積小,可是功能很強大)
下載地址:https://github.com/RubaXa/Sor...
官方DEMO:http://rubaxa.github.io/Sorta...css
npm安裝html
$ npm install sortablejs --save
bower安裝vue
$ bower install --save sortablejs
固然還有直接引入html5
<script src="../../js/Sortable.min.js"></script>
<ul id="items"> <li>item 1</li> <li>item 2</li> <li>item 3</li> </ul>
能夠經過Sorable對象中的create方法建立react
var el = document.getElementById('items'); var sortable = Sortable.create(el,{});
也能夠經過新建個Sortable對象來建立git
var sortable = new Sortable(el, {})
實例中dom結構中用到的是ul(無序列表),固然也能夠用其餘的元素來構造例如使用div等github
先把他的總體配置寫出來,在一個個介紹npm
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 object瀏覽器
string:命名,我的建議用元素id就行,用處是爲了設置能夠拖放容器時使用,在array中的put的設置中再作介紹; object:{name,pull,put} name:同string的方法, pull:pull用來定義從這個列表容器移動出去的設置,true/false/'clone'/function true:列表容器內的列表單元能夠被移出; false:列表容器內的列表單元不能夠被移出; 'clone':列表單元移出,移動的爲該元素的副本; function:用來進行pull的函數判斷,能夠進行復雜邏輯,在函數中return false/true來判斷是否移出; put:put用來定義往這個列表容器放置列表單元的的設置,true/false/['foo','bar']/function true:列表容器能夠從其餘列表容器內放入列表單元; false:與true相反; ['foo','bar']:這個能夠是一個字符串或者是字符串的數組,表明的是group配置項裏定義的name值; function:用來進行put的函數判斷,能夠進行復雜邏輯,在函數中return false/true來判斷是否放入;
事件:
事件對象:
事件對象在各個函數中略有不一樣,可經過輸出對象查看對象的屬性,下面簡單列舉幾個:
有問題加我qq吧,這個評論翻着不是很好找,635905156