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

基于 uni-app + <movable-view>拖拽实现的标签排序-适用于微信小程序、H5等多端

在实际业务中,我们经常遇到「标签排序」或「菜单调整」的场景。微信小程序原生的 movable-view 为我们提供了一个简单、高效的拖拽能力,结合 Vue3 + uni-app 的组合,我们可以实现一个体验良好的标签管理界面

核心组件<movable-view><movable-area>

1.每项数据结构加入 y 坐标

const itemHeight = uni.upx2px(110); // 每项高度,决定拖动步长function initDragg(list) {return list.map((item, index) => ({...item,y: index * itemHeight, // 初始位置}));
}

2. 拖动状态管理变量

const canDrag = ref(false);          // 当前是否允许拖动(长按才激活)
const draggingIndex = ref(-1);       // 当前拖动的 item 的 index

3.用户手指按下某项

<image@touchstart="canDrag = true":src="getSpecImgUrl('sort_set')"
/>

4.进入拖动状态

function onTouchStart(index) {if (!canDrag.value) return;draggingIndex.value = index;
}

5.拖动过程中(自动触发)

function onChange(e, index) {const dy = e.detail.y;const targetIndex = Math.round(dy / itemHeight);if (targetIndex !== index && targetIndex >= 0 && targetIndex < groupList.value.length) {// 拖动项移到新位置const moved = groupList.value.splice(index, 1)[0];groupList.value.splice(targetIndex, 0, moved);draggingIndex.value = targetIndex;} else {// 仅移动视觉,不换位置groupList.value[index].y = dy;}
}

6.拖动结束

function onTouchEnd() {if (!canDrag.value) return;draggingIndex.value = -1;// 所有项重置为标准位置groupList.value.forEach((item, i) => {item.y = i * itemHeight;});// 同步顺序到后端sortLabelList(groupList.value);canDrag.value = false;
}

完整代码测试代码

<template><scroll-view scroll-y style="height: 100vh"><movable-area style="height: 1000rpx; width: 100vw; position: relative"><block v-for="(item, index) in list" :key="item.id"><movable-viewdirection="vertical"damping="50"inertia:y="item.y":style="getStyle(index)"@touchstart="onTouchStart(index)"@touchend="onTouchEnd"@change="onChange($event, index)"><view class="item">{{ item.name }}</view></movable-view></block></movable-area></scroll-view>
</template><script setup>import { ref, reactive, onMounted } from 'vue';import { debounce } from '@/utils/util';const itemHeight = 100; // 每项高度,单位 rpx(需配合实际样式调整)const list = reactive([{ id: 1, name: '任务一', y: 0 },{ id: 2, name: '任务二', y: 100 },{ id: 3, name: '任务三', y: 200 },{ id: 4, name: '任务四', y: 300 },]);let draggingIndex = ref(-1);function onTouchStart(index) {draggingIndex.value = index;}function onTouchEnd(e, index) {console.log(444);draggingIndex.value = -1;// 重置 Y 防止漂移list.forEach((item, i) => {item.y = i * itemHeight;});}// 拖拽过程中计算是否需要交换function onChange(e, index) {// const bounce = debounce(foo,500);// bounce()const dy = e.detail.y;const targetIndex = Math.round(dy / itemHeight);console.log(2222,e,index,targetIndex);if (targetIndex !== index &&targetIndex >= 0 &&targetIndex < list.length) {console.log(3333);// 交换数据const moved = list.splice(index, 1)[0];list.splice(targetIndex, 0, moved);// 重新设置 y 值// list.forEach((item, i) => {//   item.y = i * itemHeight;// });draggingIndex.value = targetIndex;} else {list[index].y = dy;}}function getStyle(index) {return `position: absolute; left: 0; width: 100%; height: ${itemHeight}rpx; z-index: ${draggingIndex.value === index ? 10 : 1};`;}
</script><style scoped>.item {background-color: #f1f1f1;margin: 10rpx;border-radius: 10rpx;text-align: center;line-height: 100rpx;box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);}
</style>

http://www.xdnf.cn/news/655057.html

相关文章:

  • ESP32 LVGL btn事件、label赋值、ddlist选项读取
  • NGINX 用户标识模块 (ngx_http_userid_module) 完整配置与最佳实践指南
  • 知识宇宙-职业篇:嵌入式工程师
  • Pycatia基础代码解析——零件设计篇(一)
  • ATT衰减器(Attenuator)介绍
  • 华为OD机试真题——洞穴探险(2025A卷:200分)Java/python/JavaScript/C/C++/GO最佳实现
  • BGP配置命令详细框架
  • 营销推广需要解决哪些问题?
  • IP SSL证书:为IP地址提供安全加密的专业解决方案
  • 【论文解读】STaR:不用人类思维链指导,模型可以自我进化!
  • Go Web框架选型与实践:基于Gin的REST API开发指南
  • 【R语言科研绘图-最小二乘法】
  • 【混合动力能量管理新突破:负载识别优化策略深度解析与仿真实战】
  • [yolov11改进系列]基于yolov11引入级联群体注意力机制CGAttention的python源码+训练源码
  • 鸿蒙OSUniApp 实现带有滑动删除的列表#三方框架 #Uniapp
  • 基于GitHub Actions+SSH+PM2的Node.js自动化部署全流程指南
  • Nacos集群
  • 【向量数据库选型实战】FAISS vs Chroma vs Milvus vs Qdrant 全面对比
  • 【QT】QString和QStringList去掉空格的方法总结
  • day38python打卡
  • 构建版本没mac上传APP方法
  • 华为OD机试真题——猴子吃桃/爱吃蟠桃的孙悟空(2025B卷:200分)Java/python/JavaScript/C++/C语言/GO六种最佳实现
  • 【C++篇】list模拟实现
  • Qt qml Network error问题
  • 「读书报告」内网安全攻防
  • 每日算法-250526
  • GitLab 18.0 正式发布,15.0 将不再受技术支持,须升级【三】
  • 消防营区管理升级:豪越科技智能仓储与装备管理的力量
  • 【Java项目测试报告】:在线音乐平台(Online-Music)
  • 开发过的一个Coding项目