当前位置:网站首页>拖拽改变顺序
拖拽改变顺序
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
边栏推荐
- Make Jar, Not War
- C language instance_ four
- The difference between spin and sleep
- AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
- Case development of landlord fighting game
- What does security capability mean? What are the protection capabilities of different levels of ISO?
- c语言—数组
- C语言实例_2
- C语言实例_4
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
猜你喜欢
如何管理分布式团队?
AcWing 361. 观光奶牛 题解(spfa求正环)
AI automatically generates annotation documents from code
[case sharing] basic function configuration of network loop detection
2022 Google CTF SEGFAULT LABYRINTH wp
go-zero微服务实战系列(九、极致优化秒杀性能)
今日问题-2022/7/4 lambda体中修改String引用类型变量
How to manage distributed teams?
LeetCode:1175. 质数排列
Instructions for using the domain analysis tool bloodhound
随机推荐
系统休眠文件可以删除吗 系统休眠文件怎么删除
Neon Optimization: performance optimization FAQ QA
The difference between spin and sleep
C language instance_ two
Using the entry level of DVA in taro3.*
Dark horse notes - exception handling
C # method of calculating lunar calendar date 2022
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
go-zero微服务实战系列(九、极致优化秒杀性能)
[chip scheme design] pulse oximeter
json学习初体验–第三者jar包实现bean、List、map创json格式
What are the differences between Oracle Linux and CentOS?
Share a general compilation method of so dynamic library
What does security capability mean? What are the protection capabilities of different levels of ISO?
C language instance_ four
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
Neon Optimization: an instruction optimization case of matrix transpose
Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
Realize incremental data synchronization between MySQL and ES
【C语言进阶篇】指针的8道笔试题