最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

vue拖拽排序插件vuedraggable使用方法詳解

來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 22:03:16
文檔

vue拖拽排序插件vuedraggable使用方法詳解

vue拖拽排序插件vuedraggable使用方法詳解:大家好,最近做的項目要用到拖拽排序,我現(xiàn)在的項目是vue項目,所以我就屁顛屁顛的去百度有木有這樣功能的插件,我就知道一定會有,那就是vuedraggable,這是一款很棒的拖拽插件,下面我來說一下怎么引入 首先在vue項目中,用npm包下載下來 npm ins
推薦度:
導讀vue拖拽排序插件vuedraggable使用方法詳解:大家好,最近做的項目要用到拖拽排序,我現(xiàn)在的項目是vue項目,所以我就屁顛屁顛的去百度有木有這樣功能的插件,我就知道一定會有,那就是vuedraggable,這是一款很棒的拖拽插件,下面我來說一下怎么引入 首先在vue項目中,用npm包下載下來 npm ins

大家好,最近做的項目要用到拖拽排序,我現(xiàn)在的項目是vue項目,所以我就屁顛屁顛的去百度有木有這樣功能的插件,我就知道一定會有,那就是vuedraggable,這是一款很棒的拖拽插件,下面我來說一下怎么引入

首先在vue項目中,用npm包下載下來

npm install vuedraggable -S

下載下來后,引入插件,在你的vue文件的script標簽里面這樣引入

import draggable from 'vuedraggable'

別忘了下面要注冊組件

components: {
 draggable
},

然后就可以在template標簽里面使用了

<draggable v-model="colors" @update="datadragEnd" :options = "{animation:500}">
 <transition-group>
 <div v-for="element in colors" :key="element.text" class = "drag-item">
 {{element.text}}
 </div>
 </transition-group>
 </draggable>

下面貼一下詳細用法

<template>
 <draggable v-model="colors" @update="datadragEnd" :options = "{animation:500}">
 <transition-group>
 <div v-for="element in colors" :key="element.text" class = "drag-item">
 {{element.text}}
 </div>
 </transition-group>
 </draggable>
</template>

<script>
 import draggable from 'vuedraggable'
 export default{
 data(){
 return{
 msg:"這是測試組件",
 colors: [
 {
 text: "Aquamarine",
 }, 
 {
 text: "Hotpink",
 }, 
 {
 text: "Gold",
 }, 
 {
 text: "Crimson",
 }, 
 {
 text: "Blueviolet",
 },
 {
 text: "Lightblue",
 }, 
 {
 text: "Cornflowerblue",
 }, 
 {
 text: "Skyblue",
 }, 
 {
 text: "Burlywood",
 }
 ],
 startArr:[],
 endArr:[],
 count:0,
 }
 },
 components: {
   draggable
 },
 methods:{
 getdata (evt) {
 console.log(evt.draggedContext.element.text)
 },
 datadragEnd (evt) {
 evt.preventDefault();
 console.log('拖動前的索引 :' + evt.oldIndex)
 console.log('拖動后的索引 :' + evt.newIndex)
 console.log(this.colors);
 }
 },
 mounted () {
 //為了防止火狐瀏覽器拖拽的時候以新標簽打開,此代碼真實有效
 document.body.ondrop = function (event) {
 event.preventDefault();
 event.stopPropagation();
 }
 }
 }
</script>

<style lang="scss" scoped>
 .test{
 border:1px solid #ccc;
 }
 .drag-item{
 width: 200px;
 height: 50px;
 line-height: 50px;
 margin: auto;
 position: relative;
 background: #ddd;
 margin-top:20px;
 }
 .ghostClass{
 opacity: 1;
 }
 .bottom{
 width: 200px;
 height: 50px;
 position: relative;
 background: blue;
 top:2px;
 left: 2px;
 transition: all .5s linear;
 }
</style>

下面是結(jié)果

上下是可以拖動的,只是截圖的話看不出效果來,小伙伴們注意了,里面有個options選項,這個選項怎么來的呢,據(jù)我觀察這個插件是基于sortable.js,所以這個options里面的配置,和sortable.js是一樣的,下面我貼兩個地址,一個是vuedraggable的GitHub地址,一個是sortable.js的GitHub地址

vuedraggable: 學習地址 

sortable.js:學習地址

options配置如下

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
 touchStartThreshold: 0, // px, how many pixels the point should move before cancelling a delayed drag event
 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)
 preventOnFilter: true, // Call `event.preventDefault()` when triggered `filter`
 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, touchEvt, hoverTargetEl) { ... }, // 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) {
 var itemEl = evt.item; // dragged HTMLElement
 evt.to; // target list
 evt.from; // previous list
 evt.oldIndex; // element's old index within old parent
 evt.newIndex; // element's new index within new parent
 },

 // Element is dropped into the list from another list
 onAdd: function (/**Event*/evt) {
 // same properties as onEnd
 },

 // Changed sorting within list
 onUpdate: function (/**Event*/evt) {
 // same properties as onEnd
 },

 // Called by any change to the list (add / update / remove)
 onSort: function (/**Event*/evt) {
 // same properties as onEnd
 },

 // Element is removed from the list into another list
 onRemove: function (/**Event*/evt) {
 // same properties as onEnd
 },

 // 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;
 }
});

好了,今天的介紹就到這里啦,快去試試吧。

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

vue拖拽排序插件vuedraggable使用方法詳解

vue拖拽排序插件vuedraggable使用方法詳解:大家好,最近做的項目要用到拖拽排序,我現(xiàn)在的項目是vue項目,所以我就屁顛屁顛的去百度有木有這樣功能的插件,我就知道一定會有,那就是vuedraggable,這是一款很棒的拖拽插件,下面我來說一下怎么引入 首先在vue項目中,用npm包下載下來 npm ins
推薦度:
標簽: 使用 VUE 使用方法
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top