课程卡片列表要做两件事:长按上下拖动换位置,左右滑动露出删除按钮。
页面没有引入 movable-view,完全靠 touchstart/move/end 记录起点与实时坐标,再让每个卡片 position: absolute、top 由行号算出来。
真正的换算只有两条:水平位移决定删除按钮露出多少,垂直位移除以行高决定跨了几行。
坑集中在两处——touch 事件给的坐标是 px、样式里写的是 rpx;以及 id 既当数据标识又当行号,重排时必须同步改。
一、背景
需求很朴素:一个课程列表,长按某一项可以上下拖动改变顺序,向左滑动可以把这一项后面的删除按钮滑出来。
不用 movable-view 的原因也很实际——movable-view 解决的是「一个元素在容器里自由移动」,它不会帮你重排数组,也不会给你行位置的吸附逻辑,最后仍然要自己算落点。所以这个页面干脆把列表项全部绝对定位,top 直接由行号乘行高算出来,拖拽结束时改行号就等于完成了重排。
数据结构因此带一点「兼职」设计:
1 2 3 4 5 6
| interface CourseItem { id: number right: number courseName: string coursePrice: number }
|
二、实现
2.1 页面数据与初始列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| Page({ data: { windowWidth: 375, dragging: false, pageX: 0, pageY: 0, movePageX: 0, movePageY: 0, courseList: [] as CourseItem[], },
onLoad() { const { windowWidth } = wx.getWindowInfo() this.setData({ windowWidth, courseList: [ { id: 0, right: 0, courseName: '课程一', coursePrice: 199 }, { id: 1, right: 0, courseName: '课程二', coursePrice: 299 }, { id: 2, right: 0, courseName: '课程三', coursePrice: 399 }, ], }) }, })
|
2.2 记录起点与实时坐标
三个触摸事件只做一件事:把坐标存下来。这里要给 e.touches[0] 加空值保护——bindlongpress、bindtouchend 在某些时机下 touches 是空数组,直接解构会得到 undefined,再取 .pageX 就抛异常。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| dragStart(e: any) { const touch = e.touches && e.touches[0] if (!touch) return this.setData({ dragging: false, pageX: touch.pageX, pageY: touch.pageY, movePageX: touch.pageX, movePageY: touch.pageY, }) },
dragMove(e: any) { const touch = e.touches && e.touches[0] if (!touch) return this.setData({ movePageX: touch.pageX, movePageY: touch.pageY }) },
|
2.3 长按只负责「进入拖拽态」
1 2 3 4 5 6 7 8 9
| longpress(e: any) { const touch = e.touches && e.touches[0] if (!touch) return this.setData({ dragging: true, pageY: touch.pageY, movePageY: touch.pageY, }) },
|
dragStart 会先于 longpress 触发,所以 pageX/movePageX 这时已经就位,长按只需要标记状态并把垂直基点对齐到当前手指。
2.4 用位移算行号并重排
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| dragEnd(e: any) { const { id } = e.mark const { pageX, pageY, movePageX, movePageY, courseList, windowWidth, dragging } = this.data if (!courseList.length) return
const maxRight = (120 * windowWidth) / 750 const dx = pageX - movePageX const right = dx > 0 ? Math.min(dx, maxRight) : 0
const rowHeight = (120 * windowWidth) / 750 const step = dragging ? Math.round((movePageY - pageY) / rowHeight) : 0
const from = id const to = Math.max(0, Math.min(courseList.length - 1, from + step))
const list = courseList.slice() const [moved] = list.splice(from, 1) moved.right = right list.splice(to, 0, moved) list.forEach((item, index) => { item.id = index })
this.setData({ courseList: list, dragging: false }) },
|
dragging 这个开关很关键:没长按就直接上下滑,step 强制为 0,只是纯滚动或轻微的坐标漂移,不会误触发重排;横向滑动则随时可用,因为侧滑删除本来就该和长按分开。
moved.right = right 之后再重新编号,删除按钮的展开状态跟着卡片一起搬走,不会留在原来的行上。
2.5 结构与样式
卡片和删除按钮是两层:删除按钮固定在右侧,被上层的白色卡片盖住;卡片整体向左挪 right 像素后,按钮就露出来了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <block wx:for="{{courseList}}" wx:key="id"> <view class="course-warn" style="top: {{item.id * 120}}rpx">删除</view>
<view class="course-container" style="right: {{item.right}}px; top: {{item.id * 120}}rpx" mark:id="{{item.id}}" bindtouchstart="dragStart" bindtouchmove="dragMove" bindtouchend="dragEnd" bindlongpress="longpress"> <image class="course-img" src="../../../asset/squase.jpg" mode="widthFix" lazy-load /> <view class="course-menu"> <view>{{item.courseName}}</view> <view>$ {{item.coursePrice}}</view> </view> </view> </block>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| .course-warn { position: absolute; right: 0; width: 120rpx; height: 120rpx; line-height: 120rpx; text-align: center; color: #fff; background: #ff4d4f; z-index: 0; }
.course-container { position: absolute; left: 0; width: 100%; height: 120rpx; display: flex; align-items: center; background: #fff; z-index: 1; transition: top 0.2s ease; }
|
transition: top 让换行有短暂的位移动画,比瞬间跳过去自然得多。
三、踩坑
单位必须统一。 item.right 存的是 px,所以样式里写 right: {{item.right}}px;而 top 用的是 rpx,所以触摸坐标要先按 750 / windowWidth 的比值换算回 rpx 的像素当量。两边混着写是这类手写拖拽最常见的错位来源。
边界要夹住。 from + step 可能小于 0 或大于 length - 1,splice 对越界下标不会报错但结果不符合预期,落点必须 Math.max/Math.min 夹一次。
生产环境优先用现成方案。 这套手写逻辑适合理解坐标换算,但缺少滚动容器、虚拟列表、惯性吸附,列表长了明显卡。真要做商业化列表,movable-view + movable-area 或者官方 recycle-view 才是正路。
四、小结
这个页面的全部精华就是一次减法:(movePageY - pageY) / rowHeight 得到跨行数,pageX - movePageX 得到侧滑宽度,其余都是绝对定位带来的配套工作。
理解之后可以迁移到自定义排序、侧滑菜单、看板拖拽等场景;但只要需求稍微复杂(多列、吸附、长列表),就该换成 movable-view 或虚拟列表组件。
五、踩坑清单
| 问题 |
现象 |
修法 |
pageY % movePageY |
行号随机、列表自己跳 |
改成 (movePageY - pageY) / 行高 取整 |
e.touches[0] 为空 |
长按/抬手时 Cannot read property 'pageX' of undefined |
先判空再取值 |
120rpx 当成 120px |
拖两行只跳一行,或跳两行半 |
行高按 windowWidth / 750 换算 |
id 交换留空 |
卡片重叠或出现空行 |
重排后整表重编号 |
| 长按即结算 |
一按住就触发重排 |
长按只置 dragging,结算放 touchend |