介绍
- 第一次写小程序,记录一下遇到的需求以及解决方法。可能功能不是很难,主要是做下记录。为以后遇到相同的需求做铺垫。
什么是左滑删除
- 用过
QQ
的人都知道,消息列表内,左滑单个聊天可以删除、置顶的操作。对于移动端窄小的屏幕来说,这种交互可以说是非常的节省地方。故受到了众多产品狗的喜爱。

实现原理
- 最外层一个
view
水平方向排列,里面包含一个内容区view
,一个操作区view
- 让你要展示的布局充满屏幕,通过css样式让超出的删除按钮隐藏
- 监听
touch
事件,平移布局显示和隐藏删除按钮(列表每一项中有一个isTouchMove
属性,通过监听touch
改变该属性给列表不同的样式将隐藏的按钮显示出来)

直接上代码
<view class="list-page">
<view class="list-item {{user.isTouchMove?'list-item-touch-active':''}}" wx:for="{{list}}" wx:for-item="user" wx:for-index="index" wx:key="user.id" bindtouchstart="touchstart" bindtouchmove="touchmove" data-id="{{user.id}}">
<view class="item-content">
<view class="content-name">{{user.name}}</view>
<view class="content-info">
<text>{{user.phone}}</text>
<text>{{user.sex}}</text>
</view>
</view>
<view class="item-delete">删除</view>
</view>
</view>
.list-page{
display: flex;
flex-direction: column;
border-top: 2rpx solid #f0f0f0
}
.list-item{
height: 160rpx;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 2rpx solid #f0f0f0;
}
.item-content{
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
padding: 0 20rpx 0 20rpx;
-webkit-transition: all 0.4s;
transition: all 0.4s;
-webkit-transform: translateX(180rpx);
transform: translateX(180rpx);
margin-left: -200rpx;
}
.content-info{
display: flex;
width: 100%;
justify-content: space-between;
font-size: 32rpx;
color: #999
}
.content-name{
width: 100%;
}
.list-item-touch-active .item-content{
margin-left: -100rpx;
}
.list-item-touch-active .item-content, .list-item-touch-active .item-delete {
-webkit-transform: translateX(0) !important;
transform: translateX(0) !important;
}
.item-delete{
width: 100rpx;
height: 160rpx;
display: flex;
justify-content: center;
align-items: center;
background: red;
color: #fff;
font-size: 32rpx;
-webkit-transform: translateX(180rpx);
transform: translateX(180rpx);
-webkit-transition: all 0.4s;
transition: all 0.4s;
}
const App = getApp()
Page({
data: {
list:[
{
id:1,
name:'张三',
phone:'15955040222',
sex:'男',
isTouchMove:false,
},
{
id: 2,
name: '张三',
phone: '15955040222',
sex: '男',
isTouchMove: false,
},
{
id: 3,
name: '张三',
phone: '15955040222',
sex: '男',
isTouchMove: false,
},
{
id: 4,
name: '张三',
phone: '15955040222',
sex: '男',
isTouchMove: false,
},
{
id: 5,
name: '张三',
phone: '15955040222',
sex: '男',
isTouchMove: false,
},
{
id: 6,
name: '张三',
phone: '15955040222',
sex: '男',
isTouchMove: false,
},
{
id: 7,
name: '张三',
phone: '15955040222',
sex: '男',
isTouchMove: false,
},
]
},
touchstart: function (e) {
let data = App.touch._touchstart(e, this.data.list)
this.setData({
list: data
})
},
touchmove: function (e) {
let data = App.touch._touchmove(e, this.data.list,'id')
this.setData({
list: data
})
},
})
- 对于滑动事件的处理专门封装了一个
.js
文件,防止以后还会用到。
var startX
var startY
class touch {
constructor() {
}
_touchstart(e, items) {
items.forEach(function (v, i) {
if (v.isTouchMove)
v.isTouchMove = false;
})
startX = e.changedTouches[0].clientX
startY = e.changedTouches[0].clientY
return items
}
_touchmove(e, items,key) {
const id = e.currentTarget.dataset.id,
touchMoveX = e.changedTouches[0].clientX,
touchMoveY = e.changedTouches[0].clientY,
angle = this._angle({
X: startX,
Y: startY
}, {
X: touchMoveX,
Y: touchMoveY
});
items.forEach(function (v, i) {
v.isTouchMove = false
if (Math.abs(angle) > 30) return;
if (v[key] == id) {
if (touchMoveX > startX)
v.isTouchMove = false
else
v.isTouchMove = true
}
})
return items
}
_angle(start, end) {
var _X = end.X - start.X,
_Y = end.Y - start.Y
return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
}
}
export default touch
- 然后去引用这个
touch.js
文件,在app.js
文件中
import touch from './utils/touch.js'
App({
globalData: {
userInfo: null
},
touch: new touch()
})
末尾