当前位置: 首页 > news >正文

vue2实现元素拖拽

直接使用vue-draggable插件更好
以下是模仿代码

<!--  -->
<template><div><button v-on:click="shuffle">Shuffle</button><transition-group name="flip-list" tag="ul" class="box" ref="box"><liv-for="(item, index) in list":key="item.text"class="list":class="item.classList"draggable@dragstart="dragstart(item)"@dragenter="dragenter(item)"@dragend="dragend">{{ item.text }}</li></transition-group><div>12312</div></div>
</template><script>
// 记录被拖动的元素
let source;
export default {data() {return {list: [{ text: 1 }, { text: 2 }, { text: 3 }, { text: 4 }, { text: 5 }, { text: 6 }, { text: 7 }, { text: 8 }, { text: 9 }, { text: 10 }],};},created() {this.list = this.list.map((item) => {return {...item,classList: [],};});},methods: {shuffle: function () {this.list.sort(() => Math.random() - 0.5);},dragstart(e) {source = e;setTimeout(() => {e.classList.push("moveing");}, 0);},dragenter(e) {if (e.text === source.text) return;this.changeSort(e);},dragend(e) {let _soucre = this.list.find((item) => item.text === source.text);let classIndex = _soucre.classList.findIndex((item) => item === "moveing");_soucre.classList.splice(classIndex, 1);source = null},// 与soucre元素交换位置changeSort(e) {const targetIndex = this.list.findIndex((item) => item.text === e.text);const sourceIndex = this.list.findIndex((item) => item.text === source.text);this.list.splice(targetIndex, 1, ...this.list.splice(sourceIndex, 1, this.list[targetIndex]));},},
};
</script>
<style lang="scss" scoped>
.box {display: flex;padding: 20px;
}
.list {display: flex;justify-content: center;align-items: center;width: 100px;height: 50px;margin-right: 10px;background-color: skyblue;
}.moveing {background-color: gray;
}
.flip-list-move {transition: transform 0.5s;
}.li {display: flex;justify-content: center;align-items: center;width: 100px;height: 50px;margin-bottom: 10px;background-color: skyblue;
}
</style>
http://www.xdnf.cn/news/598249.html

相关文章:

  • Git实战演练,模拟日常使用,快速掌握命令
  • “轩辕杯“云盾砺剑 CTF挑战赛web方向题解
  • 【AI论文】VisualQuality-R1:通过强化学习进行推理诱导的图像质量评估
  • 【Java】异常的初步认识
  • 1.2 Box以及 InsideBox的使用
  • python字符串
  • fastadmin添加管理员账号只能查看一个表中指定条件的数据
  • 【朝花夕拾】S32K144 backdoor key解锁后劳德巴赫或者JLINK更新app
  • Python3 批量处理银行电子回单
  • 深搜题(如何找到进入下一层深搜的条件)
  • 第十九章 ADC——电压采集
  • ZeroMQ Sockets介绍及应用示例
  • 01_消息中间件概述
  • for...in 和 for...of:用法、区别
  • Vue2 项目报错问题收录(持续更新...)
  • 101个α因子#21
  • 火语言UI组件--控件事件触发
  • Vaptcha 手势、轨迹验证码
  • idea常用配置 properties中文输出乱码
  • AI智慧高光谱遥感实战精修班暨手撕99个案例项目、全覆盖技术链与应用场景一站式提升方案
  • 车载软件架构 --- FLASH bootloader 设计要点
  • 随机链表的复制问题详解与代码实现
  • python学习打卡day33
  • 等离子体隐身技术和小型等离子体防御装置设计
  • 军事目标系列之迷彩作战人员检测数据集VOC+YOLO格式2755张1类别
  • C#中WSDL文件引用问题
  • 【接近平均分配箱子数量】2022-1-23
  • uni 常用api
  • 学习STC51单片机11(芯片为STC89C52RC)
  • 嵌入式软件架构规范之 - 分层设计