您的当前位置:首页小程序实现左滑删除效果

小程序实现左滑删除效果

2020-11-27 来源:乌哈旅游

代码如下:

html

<view class="container">
 <view class="main">
 <view class="main_item">
 <movable-area>
 <movable-view damping="100" out-of-bounds="true" direction="horizontal" x="{{x}}" animation="false" bindchange="handleMovableChange" capture-bind:touchstart="handleTouchestart" capture-bind:touchend="handleTouchend">
 <view class="main_item_content ">
 左滑删除
 </view>
 </movable-view>
 </movable-area>
 <view class="delete_btn " data-productIndex="{{index}} " bindtap="handleDeleteProduct ">删除</view>
 </view>
 </view>
</view>

css:

.container {
 padding: 0;
}
 
page {
 padding: 0;
}
 
.main {
 display: flex;
 flex-direction: column;
 align-items: center;
}
 
.main_item {
 display: flex;
 flex-direction: row;
 background: #fff;
 overflow: hidden;
}
 
movable-area {
 width: 532rpx;
 height: 201rpx;
 background: #fff;
}
 
movable-view {
 width: 716rpx;
 height: 201rpx;
}
 
.main_item_content {
 box-sizing: border-box;
 height: 201rpx;
 border-radius: 10rpx;
 line-height: 201rpx;
 color: #8e8e8e;
 padding-left: 10px;
 background: #eee;
}
 
.delete_btn {
 width: 184rpx;
 height: 201rpx;
 background-color: #8FC31F;
 border-top-right-radius: 10px;
 border-bottom-right-radius: 10px;
 color: #fff;
 font-size: 28rpx;
 text-align: center;
 line-height: 201rpx;
}

js:

// pages/listDel/index.js
Page({
 
 /**
 * 页面的初始数据
 */
 data: {
 // x轴方向的偏移
 x: 0,
 // 当前x的值
 currentX: 0
 },
 
 handleMovableChange: function(e) {
 // this.data.currentX = e.detail.x;
 this.data.currentX = e.detail.x;
 },
 
 handleTouchend: function(e) {
 this.isMove = true;
 if (this.data.currentX < -46) {
 this.data.x = -92;
 this.setData({
 x: this.data.x
 });
 } else {
 this.data.x = 0;
 this.setData({
 x: this.data.x
 });
 }
 },
 handleTouchestart: function(e) {},
 /**
 * 生命周期函数--监听页面加载
 */
 onLoad: function(options) {
 // let _this = this;
 // wx.createSelectorQuery().selectAll('.delBtn ').boundingClientRect(function(rect) {
 // _this.data.delWidth = rect[0].width;
 // }).exec();
 },
 
 /**
 * 生命周期函数--监听页面初次渲染完成
 */
 onReady: function() {
 
 },
 
 /**
 * 生命周期函数--监听页面显示
 */
 onShow: function() {
 
 },
 
 /**
 * 生命周期函数--监听页面隐藏
 */
 onHide: function() {
 
 },
 
 /**
 * 生命周期函数--监听页面卸载
 */
 onUnload: function() {
 
 },
 
 /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
 onPullDownRefresh: function() {
 
 },
 
 /**
 * 页面上拉触底事件的处理函数
 */
 onReachBottom: function() {
 
 },
 
 /**
 * 用户点击右上角分享
 */
 onShareAppMessage: function() {
 
 }
})

2:touchstart、touchmove 如果大家想了解的更清楚可以查看帮助文档的哦

显示全文