话不多说直接上demo
wxml:
<view class="list">
<view class="row" wx:for="{{list}}" wx:key="id">
<movable-area class="list_item">
<movable-view class="itmem_wrap" direction="horizontal" inertia="{{true}}" out-of-bounds="{{true}}" x="{{item.x}}" damping="{{60}}" data-index="{{index}}" bind:touchstart="touchMoveStartHandle" bindtouchmove="touchMoveEndHandle">
{{'滑动删除' + item.id}}
</movable-view>
<view class="delete_wrap">
<view class="delete_btn">删除</view>
</view>
</movable-area>
</view>
</view>
wxss:
page {
background-color: #efefef;
}
.list {
padding: 30rpx 30rpx 0;
}
.row {
width: 100%;
overflow: hidden;
margin-bottom: 30rpx;
}
.list_item {
border-radius: 12rpx;
position: relative;
left: -120rpx;
width: calc(100% + 120rpx);
height: 160rpx;
}
.itmem_wrap {
width: calc(100% - 120rpx);
height: 100%;
display: flex;
align-items: center;
justify-content: center;
position: relative;
left: 120rpx;
z-index: 2;
background-color: #fff;
}
.delete_wrap {
position: absolute;
right: 0;
top: 0;
width: 50%;
height: 100%;
background-color: rgb(219, 54, 54);
display: flex;
justify-content: flex-end;
z-index: 1;
}
.delete_btn {
width: 120rpx;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
color: #fff;
}
js:
Page({
data: {
list: [{id: 1},{id: 2},{id: 3},{id: 4},{id: 5},{id: 6}],
startX: '',
startY: ''
},
onLoad: function () {
this.setListX();
},
setListX() {
this.data.list.map(item => {
item.x = 0;
})
this.setData({
list: this.data.list
})
},
touchMoveStartHandle(e) {
if (e.touches.length == 1) {
this.setData({
startX: e.touches[0].clientX,
startY: e.touches[0].clientY
});
}
},
touchMoveEndHandle: function (e) {
var currentIndex = e.currentTarget.dataset.index,
startX = this.data.startX,
startY = this.data.startY,
touchMoveEndX = e.changedTouches[0].clientX,
touchMoveEndY = e.changedTouches[0].clientY,
angle = this.angle({
X: startX,
Y: startY
}, {
X: touchMoveEndX,
Y: touchMoveEndY
});
if (Math.abs(angle) > 50) return;
this.data.list.map((item, index) => {
if (touchMoveEndX > startX) {
if (index == currentIndex) item.x = 0;
} else {
item.x = -120
if (index != currentIndex) item.x = 0;
}
})
this.setData({
list: this.data.list
})
},
angle: function (start, end) {
var _X = end.X - start.X,
_Y = end.Y - start.Y
return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
}
})