Vue3中使用vuedraggable拖拽實(shí)戰(zhàn)教程
Vue3中簡(jiǎn)單使用vuedraggable拖拽
一、安裝引入
npm i vuedraggable@4.1.0 --save
import Draggable from 'vuedraggable';
二 、使用
<template> <button @click="submit">提交數(shù)據(jù)</button> <div class="drag-box"> <Draggable v-model="tags" :list="list" :animation="100" item-key="id" class="list-group" :forceFallback="true" ghost-class="ghost" @change="onMoveCallback" :move="getdata" > <template #item="{ element }"> <div class="items"> <div class="title">{{ element.label }}</div> </div> </template> </Draggable> </div> </template> <script lang="ts"> import { reactive} from 'vue'; import Draggable from 'vuedraggable'; export default { components: { Draggable, }, setup() { let list = reactive([ { label: '模塊1', id: 1, isflod: false, }, { label: '模塊2', id: 2, isflod: false, }, { label: '模塊3', id: 3, isflod: false, }, { label: '模塊4', id: 4, isflod: false, }, { label: '模塊5', id: 5, isflod: false, }, { label: '模塊6', id: 6, isflod: false, }, ]); const onMoveCallback = (val) => { console.log('拖動(dòng)前的索引 :' + val.moved.newIndex); console.log('拖動(dòng)后的索引 :' + val.moved.oldIndex); }; const getdata = (val) => { console.log(val.draggedContext.element.id); }; // 查看最新的數(shù)據(jù) const submit = () => { console.log(list); }; // watch( // list, // (newValue, oldValue) => { // console.log('list', newValue, oldValue); // }, // { immediate: true } // ); return { list, onMoveCallback, getdata, submit, }; }, }; </script> <style scoped> .drag-box .items { display: flex; justify-content: space-between; width: 80%; padding: 20px; margin-bottom: 20px; background: #e3e3e3; border-radius: 8px; } </style>
三、Api
group: “name”, // or { name: “…”, pull: [true, false, clone], put: [true, false, array] } name相同的組可以互相拖動(dòng)
sort: true, // 內(nèi)部排序列表
delay: 0, // 以毫秒為單位定義排序何時(shí)開(kāi)始。
touchStartThreshold: 0, // px,在取消延遲拖動(dòng)事件之前,點(diǎn)應(yīng)該移動(dòng)多少像素?
disabled: false, // 如果設(shè)置為真,則禁用sortable。
store: null, // @see Store
animation: 150, // ms, 動(dòng)畫(huà)速度運(yùn)動(dòng)項(xiàng)目排序時(shí),’ 0 ’ -沒(méi)有動(dòng)畫(huà)。
handle: “.my-handle”, // 在列表項(xiàng)中拖動(dòng)句柄選擇器。
filter: “.ignore-elements”, // 不導(dǎo)致拖拽的選擇器(字符串或函數(shù))
preventOnFilter: true, // 調(diào)用“event.preventDefault()”時(shí)觸發(fā)“filter”
draggable: “.item”, // 指定元素中的哪些項(xiàng)應(yīng)該是可拖動(dòng)的。
ghostClass: “sortable-ghost”, // 設(shè)置拖動(dòng)元素的class的占位符的類名。
chosenClass: “sortable-chosen”, // 設(shè)置被選中的元素的class
dragClass: “sortable-drag”, //拖動(dòng)元素的class。
dataIdAttr: ‘data-id’,
forceFallback: false, // 忽略HTML5的DnD行為,并強(qiáng)制退出。(h5里有個(gè)屬性也是拖動(dòng),這里是為了去掉H5拖動(dòng)對(duì)這個(gè)的影響
fallbackClass: “sortable-fallback”, // 使用forceFallback時(shí)克隆的DOM元素的類名。
fallbackOnBody: false, // 將克隆的DOM元素添加到文檔的主體中。(默認(rèn)放在被拖動(dòng)元素的同級(jí))
fallbackTolerance: 0, // 用像素指定鼠標(biāo)在被視為拖拽之前應(yīng)該移動(dòng)的距離。
scroll: true, // or HTMLElement
scrollFn: function(offsetX, offsetY, originalEvent, touchEvt, hoverTargetEl) { … }
scrollSensitivity: 30, // px, how near the mouse must be to an edge to start scrolling.
scrollSpeed: 10, // px
vue3 拖拽組件 vuedraggable的使用 demo
效果圖:
<template> <div class="container mover"> <!-- animation 拖拽的過(guò)渡動(dòng)畫(huà)的時(shí)間 --> <!-- onStart 開(kāi)始拖拽的事件 --> <!-- onEnd 拖拽結(jié)束的事件 --> <!-- ghost-class 當(dāng)前正在被拖拽的元素 的樣式的類名 --> <!-- sort 是否可以進(jìn)行拖拽,false 不可以進(jìn)行拖拽, 默認(rèn)為true --> <!-- delay 鼠標(biāo)按下去 多少秒之后才可以進(jìn)行拖拽 --> <!-- handle 表示 當(dāng)鼠標(biāo)落在handle指定的元素上面時(shí)才允許拖動(dòng),下面的例子中 只有鼠標(biāo)放到 + 的div 才能拖拽 --> <!-- filter 表示鼠標(biāo)落在filter指定的元素上面 不允許拖動(dòng),和 handle正好相反 --> <!-- draggable 表示樣式為item的元素才能被拖動(dòng) --> <draggable :list="list" animation="300" @start="onStart" delay="3" :sort="true" @end="onEnd" item-key="name" ghost-class="ghost" handle=".mover" filter=".unmover" draggable=".item" fallback-tolerance="80"> <template #item="{ element }"> <div class="item "> <div class="mover">+</div> {{ element.name }}{{ element.id }} <div class="mover unmover">-</div> </div> </template> </draggable> </div> </template> <script lang="ts"> import { defineComponent, reactive, toRefs } from "vue" import draggable from "vuedraggable"; export default defineComponent({ name: "HomeIndex", components: { draggable }, setup() { const state = reactive({ list: [ { name: '1', id: 1, }, { name: '1', id: 2, }, { name: '3', id: 3, }, { name: '4', id: 4, }, { name: '5', id: 5, disabled: false }, ] }) const onStart = () => { console.log('%c 開(kāi)始拖', 'color:red'); } const onEnd = () => { console.log('%c 結(jié)束拖', 'color:green'); } return { ...toRefs(state), onStart, onEnd } } }) </script> <style lang="less" scoped> .container { width: 100vw; height: 100vh; background: salmon; .item { width: 80%; height: 5vh; background: skyblue; border: 2px solid red; margin: 20px; display: flex; justify-content: space-between; .mover { width: 50px; height: 100%; background: red; font-size: 25px; text-align: center; } .unmover { width: 50px; height: 100%; background: red; font-size: 20px; text-align: center; float: right; } } .ghost { background: #0333f2 !important; } } </style>
到此這篇關(guān)于Vue3中簡(jiǎn)單使用vuedraggable拖拽的文章就介紹到這了,更多相關(guān)Vue3使用vuedraggable拖拽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用element-ui設(shè)置table組件寬度(width)為百分比
這篇文章主要介紹了使用element-ui設(shè)置table組件寬度(width)為百分比方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vue中的事件觸發(fā)(emit)及監(jiān)聽(tīng)(on)問(wèn)題
這篇文章主要介紹了vue中的事件觸發(fā)(emit)及監(jiān)聽(tīng)(on)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue移動(dòng)端項(xiàng)目實(shí)現(xiàn)使用手機(jī)預(yù)覽調(diào)試操作
這篇文章主要介紹了Vue移動(dòng)端項(xiàng)目實(shí)現(xiàn)使用手機(jī)預(yù)覽調(diào)試操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07Vue多種方法實(shí)現(xiàn)表頭和首列固定的示例代碼
本篇文章主要介紹了Vue多種方法實(shí)現(xiàn)表頭和首列固定的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02vue+webpack實(shí)現(xiàn)異步組件加載的方法
下面小編就為大家分享一篇vue+webpack實(shí)現(xiàn)異步組件加載的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02使用vuex的時(shí)候,出現(xiàn)this.$store為undefined問(wèn)題
這篇文章主要介紹了使用vuex的時(shí)候,出現(xiàn)this.$store為undefined問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06