尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
uniapp实现列表拖拽排序效果
一、前言本文总结如何实现一个流畅的列表拖拽排序效果类似各大音乐APP的歌单排序功能。二、实现思路核心思路拖拽时只做视觉位移松手后才真正改变数据顺序三、需要改动的地方1. 数据状态准备在data中添加拖拽状态管理data() { return { // 你的列表数据 list: [], // 拖拽状态 dragState: { isDragging: false, // 是否正在拖拽 startY: 0, // 起始Y坐标 startIndex: -1, // 拖拽起始索引 hoverIndex: -1, // 当前悬停索引 movedY: 0 // 移动的距离 }, itemHeight: 80 // 每项的高度(px) } }2. 模板修改给列表项添加动态 class标记拖拽中的项动态 style控制位移触摸事件template div classlist-container div v-for(item, index) in list :keyitem.id classlist-item :class{ dragging: dragState.startIndex index dragState.isDragging } :stylegetItemStyle(index) touchstart.stoponTouchStart($event, index) touchmove.stop.preventonTouchMove($event, index) touchend.stoponTouchEnd($event, index) !-- 列表项内容 -- div classitem-content{{ item.name }}/div /div /div /template3. 样式修改添加过渡动画和拖拽状态样式.list-container { position: relative; } .list-item { /* 基础样式 */ padding: 16px; margin-bottom: 12px; background: #f5f5f5; border-radius: 8px; /* 关键过渡动画 */ transition: transform 0.4s cubic-bezier(0.25, 0.8, 0.25, 1), box-shadow 0.3s ease; position: relative; will-change: transform; } .list-item.dragging { /* 拖拽中的样式 */ background: #fff; box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2); z-index: 100; /* 拖拽项在最上层 */ }4. 添加核心方法方法一计算每个项的样式getItemStyle(index) { if (!this.dragState.isDragging) { return ; } const { startIndex, hoverIndex, movedY } this.dragState; const itemHeight this.itemHeight; // 拖拽中的项跟随手指移动 if (index startIndex) { return transform: translateY(${movedY}px) scale(1.02); z-index: 100; transition: none;; } // 其他项让开位置 if (startIndex hoverIndex) { // 向下拖拽 if (index startIndex index hoverIndex) { return transform: translateY(-${itemHeight}px);; } } else if (startIndex hoverIndex) { // 向上拖拽 if (index hoverIndex index startIndex) { return transform: translateY(${itemHeight}px);; } } return ; }方法二拖拽开始onTouchStart(e, index) { this.dragState.isDragging true; this.dragState.startIndex index; this.dragState.hoverIndex index; this.dragState.startY e.touches[0].clientY; this.dragState.movedY 0; }方法三拖拽移动onTouchMove(e, index) { if (!this.dragState.isDragging) return; const currentY e.touches[0].clientY; const deltaY currentY - this.dragState.startY; this.dragState.movedY deltaY; // 计算当前悬停位置 const offsetIndex Math.round(deltaY / this.itemHeight); let newHoverIndex this.dragState.startIndex offsetIndex; // 边界处理 newHoverIndex Math.max(0, Math.min(newHoverIndex, this.list.length - 1)); if (newHoverIndex ! this.dragState.hoverIndex) { this.dragState.hoverIndex newHoverIndex; } }方法四拖拽结束onTouchEnd(e, index) { const { startIndex, hoverIndex } this.dragState; // 先重置拖拽状态触发归位动画 this.dragState.isDragging false; this.dragState.movedY 0; // 真正改变数据顺序 if (startIndex ! hoverIndex hoverIndex 0) { const item this.list.splice(startIndex, 1)[0]; this.list.splice(hoverIndex, 0, item); } // 清理状态 this.dragState.startIndex -1; this.dragState.hoverIndex -1; }四、完整示例template div classpage h3我的歌单/h3 div classlist-container div v-for(song, index) in songList :keysong.id classsong-item :class{ dragging: dragState.startIndex index dragState.isDragging } :stylegetItemStyle(index) touchstart.stoponTouchStart($event, index) touchmove.stop.preventonTouchMove($event, index) touchend.stoponTouchEnd($event, index) div classsong-cover{{ song.emoji }}/div div classsong-info div classsong-name{{ song.name }}/div div classsong-artist{{ song.artist }}/div /div /div /div /div /template script export default { data() { return { songList: [ { id: 1, name: 晴天, artist: 周杰伦, emoji: }, { id: 2, name: 稻香, artist: 周杰伦, emoji: }, { id: 3, name: 七里香, artist: 周杰伦, emoji: }, { id: 4, name: 夜曲, artist: 周杰伦, emoji: }, { id: 5, name: 青花瓷, artist: 周杰伦, emoji: } ], dragState: { isDragging: false, startY: 0, startIndex: -1, hoverIndex: -1, movedY: 0 }, itemHeight: 76 } }, methods: { getItemStyle(index) { if (!this.dragState.isDragging) return ; const { startIndex, hoverIndex, movedY } this.dragState; if (index startIndex) { return transform: translateY(${movedY}px) scale(1.02); z-index: 100; transition: none;; } if (startIndex hoverIndex) { if (index startIndex index hoverIndex) { return transform: translateY(-${this.itemHeight}px);; } } else if (startIndex hoverIndex) { if (index hoverIndex index startIndex) { return transform: translateY(${this.itemHeight}px);; } } return ; }, onTouchStart(e, index) { this.dragState.isDragging true; this.dragState.startIndex index; this.dragState.hoverIndex index; this.dragState.startY e.touches[0].clientY; this.dragState.movedY 0; }, onTouchMove(e, index) { if (!this.dragState.isDragging) return; const deltaY e.touches[0].clientY - this.dragState.startY; this.dragState.movedY deltaY; const offsetIndex Math.round(deltaY / this.itemHeight); let newHoverIndex this.dragState.startIndex offsetIndex; newHoverIndex Math.max(0, Math.min(newHoverIndex, this.songList.length - 1)); if (newHoverIndex ! this.dragState.hoverIndex) { this.dragState.hoverIndex newHoverIndex; } }, onTouchEnd(e, index) { const { startIndex, hoverIndex } this.dragState; this.dragState.isDragging false; this.dragState.movedY 0; if (startIndex ! hoverIndex hoverIndex 0) { const item this.songList.splice(startIndex, 1)[0]; this.songList.splice(hoverIndex, 0, item); } this.dragState.startIndex -1; this.dragState.hoverIndex -1; } } } /script style scoped .page { padding: 20px; } .list-container { position: relative; margin-top: 20px; } .song-item { display: flex; align-items: center; padding: 16px; margin-bottom: 12px; background: #f5f5f5; border-radius: 10px; transition: transform 0.4s cubic-bezier(0.25, 0.8, 0.25, 1), box-shadow 0.3s ease; position: relative; will-change: transform; } .song-item.dragging { background: #fff; box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15); z-index: 100; } .song-cover { font-size: 32px; margin-right: 12px; } .song-name { font-size: 16px; font-weight: 500; } .song-artist { font-size: 13px; color: #999; margin-top: 4px; } /style
RELATED

相关推荐

原神脚本终极指南:如何快速实现自动钓鱼和智能拾取功能

原神脚本终极指南:如何快速实现自动钓鱼和智能拾取功能

原神脚本终极指南:如何快速实现自动钓鱼和智能拾取功能 【免费下载链接】genshin-impact-script 原神脚本,包含自动钓鱼、自动拾取、自动跳过对话等多项实用功能。A Genshin Impact script includes many useful features such as automatic fishing, au…

📅 2026/11/11 4:54:22
Meshroom完全指南:免费开源3D重建软件从入门到精通

Meshroom完全指南:免费开源3D重建软件从入门到精通

Meshroom完全指南:免费开源3D重建软件从入门到精通 【免费下载链接】Meshroom Node-based Visual Programming Toolbox 项目地址: https://gitcode.com/gh_mirrors/me/Meshroom 你是否想过,只需一组普通照片,就能创建出可以360度旋转、…

📅 2026/11/11 11:46:33
Umi-OCR终极指南:3分钟掌握开源免费离线OCR的完整高效解决方案

Umi-OCR终极指南:3分钟掌握开源免费离线OCR的完整高效解决方案

Umi-OCR终极指南:3分钟掌握开源免费离线OCR的完整高效解决方案 【免费下载链接】Umi-OCR OCR software, free and offline. 开源、免费的离线OCR软件。支持截屏/批量导入图片,PDF文档识别,排除水印/页眉页脚,扫描/生成二维码。内置…

📅 2026/11/14 21:23:21
MORE NEWS

更多资讯

📰

怎样p电工证保姆级教程:学历不高怕报不上名?看完这篇全搞定

怎样p电工证保姆级教程:学历不高怕报不上名?看完这篇全搞定 你是不是也这样?学历不高,怕报名考试被拒,更怕花冤枉钱走弯路。别急,这篇文章是 保姆级教程…

📰

南宁电工证丶登高证怎么报名,实操考试心里没底怕挂科?

南宁电工证丶登高证怎么报名,实操考试心里没底怕挂科? 实操考试心里没底怕挂科?报名流程不清楚?考试内容摸不着头脑?这些问题在南宁电工证丶登高证考试中非常常见,特别是对于新手或转行的人来说,不仅报名过程让人困惑,考试准备也让人抓不住重点。今天…

📰

特种工离岗多少时间郑州报考避坑指南

特种工离岗多少时间郑州报考避坑指南 你是不是也遇到过这种情况:手里拿着特种作业操作证,但因为工作调动或转岗,离岗一段时间后想继续使用证书,却发现很多规则不清晰?特别是像郑州这样的大城市,报名渠道多、信息杂,稍不注意就容易被中介套路,甚至花冤…

📰

高级电工证值不值钱?证书过期了不知道怎么复审延期怎么办

高级电工证值不值钱?证书过期了不知道怎么复审延期怎么办 证书过期了不知道怎么复审延期,很多电工朋友都遇到过这个问题,尤其是那些在建筑工地、电力维修、工厂设备维护等一线岗位上工作的朋友。高级电工证虽然是上岗必备的资质,但很多人在拿到证之后,对…

📰

抚顺电工证报名处千万别踩坑:怕考不过白交培训费?这样备考稳过

抚顺电工证报名处千万别踩坑:怕考不过白交培训费?这样备考稳过 怕考不过白交培训费?抚顺电工证报名处千万别踩坑,别再被培训机构套路了。电工证是特种作业人员的“门槛证”,没有它连上岗都成问题。很多人以为电工证好考,结果一上考场就傻眼。今天从郑州…

📰

清溪哪里可以考电工证全国通用?外省证怎么转过来

清溪哪里可以考电工证全国通用?外省证怎么转过来 之前考的证在外省能不能转过来?这个问题在清溪地区不少电工从业者心头都悬着,特别是那些从外地转岗过来的师傅们。现在全国通用的政策已经明确,但很多人并不清楚具体怎么操作。别急,本文就来帮你解决这些…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬