小程序左滑删除

直接上代码

界面:

  <scroll-view class="scroll-wpper">
    <view wx:for="{{collections}}" class="recommended-coner-wpper"  id='{{item.objectId}}'>
      <view bindtouchstart="touchS" bindtouchmove="touchM" bindtouchend="touchE" data-index="{{index}}" style="{{item.txtStyle}}" id='{{item.objectId}}' class="list_del txt" bindtap='gamedetails'>//这段是重点  设置触点  设置左滑动作
        <view class="lime-ico">
          <image class="ico" src='{{item.pic}}'></image>
        </view>
        <view class="coner-wpper">
          <view class="coner-liem">{{item.name}}</view>
          <view class="species-wpper">
            <view class="age">{{item.ageName}}</view>
            <view class="species-type">{{item.typeName}}</view>
            <view class="species-type-coner">{{item.detailDesc}}</view>
          </view>
        </view>
      </view>
      <view data-index="{{index}}" bindtap="delItem" id="{{item.objectId}}" class="list_del del">
        <image src='{{del}}' class='del_icon' />
      </view>//删除按钮

    </view>
  </scroll-view>

 

js:

//左滑删除
  touchS: function(e) {
    console.log(e)
    if (e.touches.length == 1) {
      this.setData({ //设置触摸起始点水平方向位置         
        startX: e.touches[0].clientX
      });
    }
  },
  touchM: function(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.collections;
      //将拼接好的样式设置到当前item中
      list[index].txtStyle = txtStyle;

      //更新列表的状态
      this.setData({
        collections: 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.collections;
      list[index].txtStyle = txtStyle;
      //更新列表的状态
      that.setData({
        collections: list
      });
    }
  },

打完收工,下次直接上cv操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值