vue实现拖拽交换位置
作者:包子源 时间:2024-04-30 08:45:51
本文实例为大家分享了vue实现拖拽交换位置的具体代码,供大家参考,具体内容如下
<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>
效果
来源:https://blog.csdn.net/ziwoods/article/details/123665613
标签:vue,拖拽,交换位置
0
投稿
猜你喜欢
简述MySQL InnoDB存储引擎
2024-01-28 09:26:16
Python爬取酷狗MP3音频的步骤
2022-04-07 03:15:21
使用Python下的XSLT API进行web开发的简单教程
2022-07-24 22:07:14
CGO编程基础快速入门
2024-02-05 05:20:58
编写SQL需要注意的细节Checklist总结
2024-01-17 14:41:56
Python中staticmethod和classmethod的作用与区别
2022-03-12 21:30:00
Python线程编程之Thread详解
2022-04-10 17:37:01
jupyter notebook指定启动目录的方法
2022-05-19 18:53:01
Django前端BootCSS实现分页的方法
2023-12-21 01:45:34
Python简单遍历字典及删除元素的方法
2021-12-31 08:57:51
Python pandas的八个生命周期总结
2023-02-08 17:01:04
Python NumPy中的随机数及ufuncs函数使用示例详解
2021-09-22 15:29:08
Mac上安装MySQL过程分享
2024-01-22 16:03:34
jupyter-lab设置自启动及远程连接开发环境
2023-08-29 09:11:39
javascript、php关键字搜索函数的使用方法
2024-05-08 10:10:19
PHP 数组和字符串互相转换实现方法
2023-06-19 15:04:17
如何使用VUE+faceApi.js实现摄像头拍摄人脸识别
2023-07-02 16:32:04
python实现的各种排序算法代码
2022-06-17 05:41:19
python实现自动化报表功能(Oracle/plsql/Excel/多线程)
2021-04-02 02:05:02
javascript实现日期3级联动下拉框选择菜单
2023-10-19 02:15:34