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

uniapp结合movable-area与movable-view实现拖拽功能

前言

因为公司业务开发需要拖拽功能。

ps:该功能只能针对高度一致的,如果高度不一致需要另外二开

演示

开始

<template><view style="height: 100%;"><movable-area :style="{'width': '100%', 'height': allHeight + 'px'}"><movable-viewv-for="(item, index) in list":key="item.id":x="0":y="item.y":style="{'height': itemHeight + 'px', 'width': '100%',}"direction="all"@touchstart="handleDragStart(index)"@change="handleMoving(index, $event)"@touchend="handleDragEnd"class="movable-view"><!-- 这里可以放置步骤的详细内容 --><view style="background-color: aqua; padding: 20rpx 0;">{{ item.desc }}</view></movable-view></movable-area></view>
</template><script>
export default {data() {return {list: [],cloneList: [],activeIndex: -1, // 选中oldIndex: -1,moveToIndex: -1, // 移动allHeight: 300,itemHeight: 50};},created() {for(let i = 0; i < 12; i ++) {let info = {id: i,desc: '测试' + i}this.list.push(info)}this.allHeight = 12 * this.itemHeightthis.initList(this.list)},methods: {deepCopy(source) {return JSON.parse(JSON.stringify(source));},initList(list=[]){const newList = this.deepCopy(list);this.list = newList.map((item, index) => {return {...item,y: index * this.itemHeight,key: Math.random() + index};});//拷贝一份初始list值this.cloneList = this.deepCopy(this.list);},// 拖拽开始handleDragStart(index) {this.activeIndex = index;this.oldIndex = index;},handleMoving(index, e){if (e.detail.source !== 'touch') return;const { x, y } = e.detail;const currentY = Math.floor((y + this.itemHeight / 2) / this.itemHeight);this.moveToIndex = Math.min(currentY, this.list.length - 1);//更新移动后的位置if (this.oldIndex !== this.moveToIndex && this.oldIndex !== -1 && this.moveToIndex !== -1) {const newList = this.deepCopy(this.cloneList);//交换位置newList.splice(this.moveToIndex, 0, ...newList.splice(this.activeIndex, 1));this.list.forEach((item, index) => {if (index !== this.activeIndex) {const itemIndex = newList.findIndex(val => val.id === item.id);item.y = itemIndex*this.itemHeight}});this.oldIndex = this.moveToIndex;}},handleDragEnd(e) {if (this.moveToIndex !== -1 && this.activeIndex !== -1 && this.moveToIndex !== this.activeIndex) {this.cloneList.splice(this.moveToIndex, 0, ...this.cloneList.splice(this.activeIndex, 1));}// 重新排序下更新后的位置。this.initList(this.cloneList);this.activeIndex = -1;this.oldIndex = -1;this.moveToIndex = -1;},},
};
</script><style>
.movable-area {}.movable-view {}
</style>

http://www.lryc.cn/news/500641.html

相关文章:

  • 十九(GIT2)、token、黑马就业数据平台(页面访问控制(token)、首页统计数据、登录状态失效)、axios请求及响应拦截器、Git远程仓库
  • 文生图模型开源之光!ComfyUI - AuraFlow本地部署教程
  • spring boot之@Import注解的应用
  • 【记录】用JUnit 4的@Test注解时报错java.lang.NullPointerException的原因与解决方法
  • Spring Boot 自动化脚本-多线程批量压缩图片
  • 依托 Spring Boot框架,精铸高扩展性招聘信息管控系统
  • 【前端】理解 JavaScript 对象属性访问的复杂性
  • Django异步视图adrf解决办法
  • 【一文了解】C#基础-接口
  • 活着就好20241210
  • 多表设计 - 一对一多对多
  • 实现 DataGridView 下拉列表功能(C# WinForms)
  • 使用Java创建RabbitMQ消息生产者的详细指南
  • 【LC】160. 相交链表
  • Spark架构及运行流程
  • Linux安装Python2.7.5(centos自带同款)
  • 上传ssh公钥到目标服务器
  • 【LLMs】用LM Studio本地部署离线大语言模型
  • SpringBoot下类加入容器的几种方式
  • 【Mysql】忘记Root密码后如何不影响数据进行重置密码
  • 宝塔内设置redis后,项目以及RedisDesktopManager客户端连接不上!
  • 一文了解模式识别顶会ICPR 2024的研究热点与最新趋势
  • 【深度学习】深刻理解BERT
  • 一种基于通义千问prompt辅助+Qwen2.5-coder-32b+Bolt.new+v0+Cursor的无代码对话网站构建方法
  • Java版-图论-最小生成树-Kruskal算法
  • 计算机网络知识总结
  • 普通算法——欧拉筛
  • 【知识科普】DNS(域名解析服务)深入解读
  • 数据结构第一弹-数据结构在不同领域的应用
  • 如何创建基于udp的客户端和服务端