微信小程序左滑删除效果的实现完整源码附效果图

效果图:

功能描述,小程序列表左滑删除功能的实现完整源代码实现:

 

<view wx:for='{{friends}}' wx:key="" wx:if='{{groupType==4}}' catchtap="nav_oneChat" id="{{item._id}}" class="item p_r" style="{{item.txtStyle}}" bindtouchstart="touchS" bindtouchmove="touchM" bindtouchend="touchE" data-index="{{index}}">
		<image class="head" src="{{item.identity=='a'?(item.b_head||'/image/default_head.png'):(item.a_head||'/image/default_head.png')}}"></image>
		<view wx:if='{{item.hongdian}}' class="hongdian"></view>
		<view class="loc" wx:if='{{!item.delShow}}'>{{item.newTime}}</view>
		<view class="userName">{{item.identity=='a'?item.b_name:item.a_name}}</view>

		<!-- 左滑删除 -->
		<view bindtap="delItem" wx:if='{{item.delShow}}' class="posit">
			<view class="del" data-id="{{item._id}}" data-index="{{index}}" catchtap="del">删除</view>
		</view>

</view>

JS代码:


  //手指刚放到屏幕触发
  touchS:function(e){
    console.log("touchS"+e);
   //判断是否只有一个触摸点
    if(e.touches.length==1){
      this.setData({
        //记录触摸起始位置的X坐标
        startX:e.touches[0].clientX
      });
    }
  },
 //触摸时触发,手指在屏幕上每移动一次,触发一次
  touchM:function(e){
      console.log("touchM:"+e);
    var that = this
    if(e.touches.length==1){
     //记录触摸点位置的X坐标
      var moveX = e.touches[0].clientX;
      //计算手指起始点的X坐标与当前触摸点的X坐标的差值
      var disX = that.data.startX - moveX;
     //delBtnWidth 为右侧按钮区域的宽度
      var delBtnWidth = that.data.delBtnWidth;
      var txtStyle = "";
      if(disX == 0 || disX < 0){//如果移动距离小于等于0,文本层位置不变
        txtStyle = "left:0px";
      }else if(disX > 0 ){//移动距离大于0,文本层left值等于手指移动距离
        txtStyle = "left:-"+disX+"px";
        if(disX>=delBtnWidth){
          //控制手指移动距离最大值为删除按钮的宽度
          txtStyle = "left:-"+delBtnWidth+"px";
        }
      }
      //获取手指触摸的是哪一个item
      var index = e.currentTarget.dataset.index;
      var list = that.data.friends;
      //将拼接好的样式设置到当前item中
      list[index].txtStyle = txtStyle; 
      list.forEach(item=>item.delShow=false)
      list[index].delShow = true; 
      //更新列表的状态
      this.setData({
       friends:list
      });
    }
  },
  touchE:function(e){
      console.log("touchE"+e);
    var that = this
    if(e.changedTouches.length==1){
      //手指移动结束后触摸点位置的X坐标
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束,手指移动的距离
      var disX = that.data.startX - endX;
      var delBtnWidth = that.data.delBtnWidth;
      //如果距离小于删除按钮的1/2,不显示删除按钮
      var txtStyle = disX > delBtnWidth/2 ? "left:-"+delBtnWidth+"px":"left:0px";
      //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index;
      var list = that.data.friends;
      list[index].txtStyle = txtStyle; 
      //更新列表的状态
      that.setData({
       friends:list
      });
    }
  },

css (样式部分可用自己的,这里仅供参考)

/* pages/chatList/chatList.wxss */
page {
  background-color: #EDEDED;
}
.posit{
  padding: 50rpx 10rpx 50rpx;
  background-color: white;
}
.del{
  width: 200rpx;
  height: 60rpx;
  line-height: 60rpx;
  text-align: center;
  background-color: red;
  z-index: 2;
  color: white;
}



.groupType{
  text-align: center;
  flex: 1;
  z-index: 999999;
}
.ring1{
  position: fixed;
  top: 30rpx;
  right: 30rpx;
  height: 40rpx;
  width: 40rpx;
  z-index:99999;
  background-color: white;
  padding:10rpx;
  border-radius: 50%;
  border: 1px solid #d0d0d0;
}
.ring2{
  
  position: fixed;
  top: 130rpx;
  right: 30rpx;
  height: 40rpx;
  width: 40rpx;
  z-index:99999;
  background-color: white;
  padding:10rpx;
  border-radius: 50%;
  border: 1px solid #d0d0d0;
}
.on{
  color: cadetblue;
}
.hongdian{
  width: 20rpx;
  height: 20rpx;
  border-radius: 50%;
  background-color: red;
  position: absolute;
  left: 96rpx;
  top: 14rpx;
}
.addIcon{
  width: 80rpx;
  height: 80rpx;
  position: fixed;
  right: 50rpx;
  bottom: 300rpx;
  border-radius: 50%;
}
.p_r{
  display: flex;
  flex-direction: row;
}
.tab{
  position: fixed;
  bottom: 0;
  border-top: 1px solid #d0d0d0;
  height: 90rpx;
  line-height: 90rpx;
  z-index: 999999;
  background-color: white;
  width: 100%;
}
.top{
  position: fixed;
  top: 0;
  z-index: 2;
  width: 100%;
  background-color: #EDEDED;
}
.pageTitle {
  padding-top: 60rpx;
  padding-left: 20rpx;
  height: 140rpx;
  background-color: #EDEDED;
}
 
.searchcion {
  margin: 16rpx 10rpx 10rpx 10rpx;
  position: absolute;
  right: 15rpx;
  z-index: 2;
  width: 20px;
  height: 20px;
  text-align: center;
}
 
.search_arr {
  border: 1px solid #d0d0d0;
  background-color: #EDEDED;
  border-radius: 50rpx;
  margin-left: 30rpx;
  position: fixed;
  width: 40%;
  top: 54rpx;
  left: 200rpx;
}
 
.search_arr input {
  margin-left: 20rpx;
  height: 60rpx;
  border-radius: 5px;
}
swiper{
  position: fixed;
  width: 100%;
}
.swiper-item{
  width: 100%;
  height: 300rpx;
}
.item{
  position: relative;
  margin-bottom: 20rpx;
}
.head{
  width: 90rpx;
  height: 90rpx;
  min-width: 90rpx;
  border-radius: 10rpx;
  margin: 16rpx;
}
.user_list{
  position: relative;
  padding: 20rpx 0 20rpx;
  background-color: white;
  margin-bottom: 100rpx;
}
.userName{
  margin-left: 20rpx;
  border-bottom: 1rpx solid #d0d0d0;
  width: 82%;
  font-weight: normal;
  font-size: 32rpx;
  margin-top: 32rpx;
  padding-bottom: 10rpx;
}
.loc{
position: absolute;
right: 40rpx;
top: 40rpx;
}

 

相关推荐
©️2020 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页
实付 49.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值