vue拖拽組件使用方法詳解
前言
pc端開發(fā)需要拖拽組件完成列表的順序交換,一般移動端的UI組件會包含,但是我在用的iview并沒有此功能的組件,于是手寫一個,實(shí)現(xiàn)起來很簡單。效果圖如下:
可以拖拽完成新排序,點(diǎn)擊某一項(xiàng)可以觸發(fā)相關(guān)事件.
關(guān)于拖拽 drag & drop
拖放(Drag 和 drop)是 HTML5 標(biāo)準(zhǔn)的組成部分。
拖拽對象
dataTransfer對象,只能在拖放事件的事件處理程序中訪問。重要屬性:
- effectAllowed ( none | copy | copyLink | copyMove | link、linkMove | move | all | uninitialized ):設(shè)置或返回被拖動元素允許發(fā)生的拖動行為。
- dropEffect( none | copy | link | move ):設(shè)置或返回拖放目標(biāo)上允許發(fā)生的拖放行為。如果此設(shè)置的拖放行為不在effectAllowed屬性設(shè)置的多種拖放行為之內(nèi),拖放操作將會失敗。
- dataTransfer.getData(format):獲取DataTransfer對象中設(shè)置format格式的數(shù)據(jù)。其中format代表數(shù)據(jù)格式,data代表數(shù)據(jù)。
拖拽屬性
draggable 屬性規(guī)定元素是否可拖動。
拖拽事件
- ondragstart:在拖動開始時執(zhí)行,返回被拖動元素。
- ondragover:返回在何處放置被拖動的數(shù)據(jù)
- 默認(rèn)地,無法將數(shù)據(jù)/元素放置到其他元素中。如果需要設(shè)置允許放置,我們必須阻止對元素的默認(rèn)處理方式
- ondragenter:在被拖動的元素進(jìn)入到放置目標(biāo)時執(zhí)行
- ondragleave:在被拖動的元素離開放置目標(biāo)時執(zhí)行
- ondragend && ondrop:皆指鼠標(biāo)松開被拖動對象的事件,但是返回的分別為被拖動對象和被拖動元素懸掛的那個元素
源碼:
<template> <div class="transition-container"> <div class="item" v-for="(item, index) in items" :key="index" draggable="true" @dragstart="handleDragStart($event, item)" @dragover.prevent="handleDragOver($event, item)" @dragenter="handleDragEnter($event, item)" @dragend="handleDragEnd($event, item)" @click="chooseNav(item)" > <p class="trans-btn"> <span v-if="item.problemId"> <b class="id"> {{item.problemId}} </b> {{item.key}} </span> <span v-else> {{item.key}} </span> <span> <i-button v-if="btn" size="small" type="error" style="margin-right: 10px;" @click="deleteItem(item, index)">刪除</i-button> </span> </p> </div> </div> </template> <script> import './index.less'; export default { name: 'transition', props: { dataSource: Array, btn: Boolean, }, data() { return { items: [], dragging: null, }; }, watch: { dataSource(val) { this.items = val; }, dragging(val) { if (this.dataSource.includes(val)) { this.dragging = val; } else { this.dragging = null; } }, }, methods: { handleDragStart(e, item) { this.dragging = item; }, handleDragEnd() { this.dragging = null; this.$emit('hasChanged', this.items); }, // 首先把div變成可以放置的元素,即重寫dragenter/dragover handleDragOver(e) { e.dataTransfer.dropEffect = 'move';// e.dataTransfer.dropEffect="move";//在dragenter中針對放置目標(biāo)來設(shè)置! }, handleDragEnter(e, item) { if (this.dragging) { e.dataTransfer.effectAllowed = 'move';// 為需要移動的元素設(shè)置dragstart事件 if (item === this.dragging) { return; } const newItems = [...this.items]; console.log(newItems); const src = newItems.indexOf(this.dragging); const dst = newItems.indexOf(item); newItems.splice(dst, 0, ...newItems.splice(src, 1)); this.items = newItems; } }, chooseNav(val) { this.$emit('selectItem', val); }, deleteItem(item, index) { this.$emit('deleteItem', item, index); }, editor(item, index) { this.$emit('editorItem', item, index); }, }, }; </script>
基本功能就完成啦
參考文章
W3school——HTML 5 拖放
div拖拽互換位置(vue)
如果簡單的功能不能滿足,推薦這個寫好的輪子
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue 可拖拽組件Vue Smooth DnD的使用詳解
- vue拖拽組件 vuedraggable API options實(shí)現(xiàn)盒子之間相互拖拽排序
- Vue拖拽組件列表實(shí)現(xiàn)動態(tài)頁面配置功能
- Vue拖拽組件開發(fā)實(shí)例詳解
- vue 實(shí)現(xiàn)拖拽動態(tài)生成組件的需求
- vue使用Split封裝通用拖拽滑動分隔面板組件
- vue開發(fā)拖拽進(jìn)度條滑動組件
- vue draggable resizable 實(shí)現(xiàn)可拖拽縮放的組件功能
- 利用Vue-draggable組件實(shí)現(xiàn)Vue項(xiàng)目中表格內(nèi)容的拖拽排序
- Vue組件Draggable實(shí)現(xiàn)拖拽功能
- Vue實(shí)現(xiàn)可拖拽組件的方法
相關(guān)文章
VUE多個下拉框?qū)崿F(xiàn)雙向聯(lián)動效果
這篇文章主要為大家詳細(xì)介紹了VUE多個下拉框?qū)崿F(xiàn)雙向聯(lián)動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07學(xué)習(xí)Vue框架中必掌握的重點(diǎn)知識
這篇文章主要介紹了學(xué)習(xí)Vue中必掌握的重點(diǎn)知識,想了解vue的同學(xué)可以參考下2021-04-04laravel5.3 vue 實(shí)現(xiàn)收藏夾功能實(shí)例詳解
這篇文章主要介紹了laravel5.3 vue 實(shí)現(xiàn)收藏夾功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-01-01Vue中的transition封裝組件的實(shí)現(xiàn)方法
這篇文章主要介紹了Vue中的transition封裝組件的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08vuex中遇到的坑,vuex數(shù)據(jù)改變,組件中頁面不渲染操作
這篇文章主要介紹了vuex中遇到的坑,vuex數(shù)據(jù)改變,組件中頁面不渲染操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11Vue3中使用reactive時,后端有返回?cái)?shù)據(jù)但dom沒有更新的解決
這篇文章主要介紹了Vue3中使用reactive時,后端有返回?cái)?shù)據(jù)但dom沒有更新的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03vue關(guān)于eslint空格縮進(jìn)等的報錯問題及解決
這篇文章主要介紹了vue關(guān)于eslint空格縮進(jìn)等的報錯問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05vue實(shí)現(xiàn)三級頁面跳轉(zhuǎn)功能
這篇文章主要介紹了vue實(shí)現(xiàn)三級頁面跳轉(zhuǎn)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05