当前位置:网站首页>拖拽改变顺序
拖拽改变顺序
2022-07-06 18:02:00 【阿六啊】
一个列表 通过拖拽 改变她的顺序
关键:draggable=“true” 可以拖拽
@dragstart=“” 开始拖拽
@drop=“”
@dragover=‘’
<template>
<div>
<li class="li_row" v-for="(item,index) in linkData " :key="index" draggable="true"
@dragstart="dragstart($event,index)" @drop="drop($event,index)" @dragover='dragover($event)'>
<div class="li_item title">{
{
item.name}}</div>
</li>
</div>
</template>
<script>
export default {
data() {
return {
linkData: [
{
name:'百度',url:'wwww.baidu.com',description:'baidu'},
{
name:'腾讯网',url:'wwww.qq.com',description:'tencent'},
{
name:'新浪微博',url:'https://www.weibo.com/',description:'weibo'},
{
name:'今日头条',url:'https://www.toutiao.com/',description:'bytedance'},
{
name:'哔哩哔哩',url:'https://www.bilibili.com/',description:'bilibili'}
]
}
},
methods: {
dragstart(e, index) {
e.dataTransfer.setData('index', index);
},
drop(event, index) {
event.preventDefault();
let startIndex = parseInt(event.dataTransfer.getData('index')); // 数据原本的index
let currentIndex = parseInt(index);// 数据现在的index
console.log("start", startIndex);
console.log("drop", currentIndex);
if (startIndex - currentIndex > 0) {
console.log("要拖拽的元素的索引 大于 当前位置的元素的索引");
this.linkData.splice(currentIndex, 0, this.linkData[startIndex]);
console.log("删除" + startIndex + 1);
this.linkData.splice(startIndex + 1, 1)
} else if (startIndex - currentIndex < 0) {
console.log("要拖拽的元素的索引 小于 当前位置的元素的索引");
this.linkData.splice(currentIndex + 1, 0, this.linkData[startIndex]);
this.linkData.splice(startIndex, 1)
} else {
console.log("什么也不用做");
}
},
// 拖动事件(主要是为了拖动时鼠标光标不变为禁止)
dragover(e) {
e.preventDefault()
},
},
}
</script>
<style lang="scss" scoped>
</style>
参考链接
https://blog.csdn.net/azuretodd/article/details/109973228
边栏推荐
- JTAG debugging experience of arm bare board debugging
- go-zero微服务实战系列(九、极致优化秒杀性能)
- LeetCode:1175. 质数排列
- Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
- uva 1401 dp+Trie
- Lldp compatible CDP function configuration
- Neon Optimization: an optimization case of log10 function
- Case development of landlord fighting game
- table表格设置圆角
- Wood extraction in Halcon
猜你喜欢
LeetCode:1175. Prime permutation
Yunna | work order management measures, how to carry out work order management
JTAG debugging experience of arm bare board debugging
云呐-工单管理制度及流程,工单管理规范
Transformation transformation operator
如何管理分布式团队?
405 method not allowed appears when the third party jumps to the website
Body mass index program, entry to write dead applet project
Typical problems of subnet division and super network construction
[signal and system]
随机推荐
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
Typical problems of subnet division and super network construction
Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
C# 计算农历日期方法 2022
Wood extraction in Halcon
NEON优化:关于交叉存取与反向交叉存取
云呐-工单管理制度及流程,工单管理规范
一起看看matlab工具箱内部是如何实现BP神经网络的
uva 1401 dp+Trie
Vocabulary in Data Book
What are the differences between Oracle Linux and CentOS?
Yunna | work order management measures, how to carry out work order management
系统休眠文件可以删除吗 系统休眠文件怎么删除
C语言实例_5
DS-5/RVDS4.0变量初始化错误
Taro2.* applet configuration sharing wechat circle of friends
Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
Metauniverse urban legend 02: metaphor of the number one player
Taro 小程序开启wxml代码压缩