vue實(shí)現(xiàn)拖拽交換位置
本文實(shí)例為大家分享了vue實(shí)現(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中針對(duì)放置目標(biāo)來(lái)設(shè)置 ? ? ? e.dataTransfer.dropEffect = "move"; ? ? }, ? ? handleDragEnter(e, item) { ? ? ? // 為需要移動(dòng)的元素設(shè)置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>
效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Vue實(shí)現(xiàn)調(diào)用接口加載頁(yè)面初始數(shù)據(jù)
今天小編就為大家分享一篇使用Vue實(shí)現(xiàn)調(diào)用接口加載頁(yè)面初始數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10Vue 中頁(yè)面?zhèn)髦档亩喾N方式小結(jié)
本文主要介紹了Vue 中頁(yè)面?zhèn)髦档亩喾N方式小結(jié),主要包括路由傳參、Vuex 狀態(tài)管理、Props 屬性和事件傳遞數(shù)據(jù)這幾種,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10vue3 provide和inject底層組件的值不是響應(yīng)式的處理詳解
這篇文章主要為大家介紹了vue3 provide和inject底層組件的值不是響應(yīng)式的處理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Vue3組件異步更新和nextTick運(yùn)行機(jī)制源碼解讀
這篇文章主要為大家介紹了Vue3組件異步更新和nextTick運(yùn)行機(jī)制源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03vue項(xiàng)目中使用axios遇到的相對(duì)路徑和絕對(duì)路徑問(wèn)題
這篇文章主要介紹了vue項(xiàng)目中使用axios遇到的相對(duì)路徑和絕對(duì)路徑問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06vue.js動(dòng)畫中的js鉤子函數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了vue.js動(dòng)畫中的js鉤子函數(shù)的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07