vue實現(xiàn)拖拽交換位置
更新時間:2022年04月07日 09:56:36 作者:包子源
這篇文章主要為大家詳細介紹了vue實現(xiàn)拖拽交換位置,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現(xiàn)拖拽交換位置的具體代碼,供大家參考,具體內(nèi)容如下
<template> ? <div class="root"> ? ? <transition-group tag="div" class="container"> ? ? ? <div ? ? ? ? class="item" ? ? ? ? :class="'item' + i" ? ? ? ? v-for="(item, i) in items" ? ? ? ? :key="item.key" ? ? ? ? :style="{ 'background-color': item.color, border: '1px solid' }" ? ? ? ? draggable="true" ? ? ? ? @dragstart="handleDragStart($event, item)" ? ? ? ? @dragover.prevent="handleDragOver($event, item)" ? ? ? ? @dragenter="handleDragEnter($event, item)" ? ? ? ? @dragend="handleDragEnd($event, item)" ? ? ? > ? ? ? ? <div>{{ item }}</div> ? ? ? </div> ? ? </transition-group> ? </div> </template> ? <script> export default { ? name: "Toolbar", ? data() { ? ? return { ? ? ? items: [ ? ? ? ? { key: 1, color: "#3883a0" }, ? ? ? ? { key: 2, color: "#4883a0" }, ? ? ? ? { key: 3, color: "#5883a0" }, ? ? ? ? { key: 4, color: "#6883a0" }, ? ? ? ? { key: 5, color: "#7883a0" }, ? ? ? ? { key: 6, color: "#8883a0" }, ? ? ? ? { key: 7, color: "#9883a0" }, ? ? ? ], ? ? ? ending: null, ? ? ? dragging: null, ? ? }; ? }, ? methods: { ? ? handleDragStart(e, item) { ? ? ? this.dragging = item; ? ? }, ? ? handleDragEnd(e, item) { ? ? ? if (this.ending.key === this.dragging.key) { ? ? ? ? return; ? ? ? } ? ? ? let newItems = [...this.items]; ? ? ? const src = newItems.indexOf(this.dragging); ? ? ? const dst = newItems.indexOf(this.ending); ? ? ? newItems.splice(src, 1, ...newItems.splice(dst, 1, newItems[src])); ? ? ? console.log(newItems); ? ? ? ? this.items = newItems; ? ? ? this.$nextTick(() => { ? ? ? ? this.dragging = null; ? ? ? ? this.ending = null; ? ? ? }); ? ? }, ? ? handleDragOver(e) { ? ? ? // 首先把div變成可以放置的元素,即重寫dragenter/dragover ? ? ? // 在dragenter中針對放置目標來設置 ? ? ? e.dataTransfer.dropEffect = "move"; ? ? }, ? ? handleDragEnter(e, item) { ? ? ? // 為需要移動的元素設置dragstart事件 ? ? ? e.dataTransfer.effectAllowed = "move"; ? ? ? this.ending = item; ? ? }, ? }, }; </script> ? <style lang="less" scoped> .container { ? display: flex; ? flex-wrap: wrap; } .item { ? width: 200px; ? height: 200px; ? margin: 10px; ? color: #fff; ? transition: all linear 0.3s; } .item0 { ? width: 400px; } </style>
效果
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用Vue實現(xiàn)調(diào)用接口加載頁面初始數(shù)據(jù)
今天小編就為大家分享一篇使用Vue實現(xiàn)調(diào)用接口加載頁面初始數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10vue3 provide和inject底層組件的值不是響應式的處理詳解
這篇文章主要為大家介紹了vue3 provide和inject底層組件的值不是響應式的處理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08vue.js動畫中的js鉤子函數(shù)的實現(xiàn)
這篇文章主要介紹了vue.js動畫中的js鉤子函數(shù)的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07