美文网首页
微信小程序开发,左滑删除

微信小程序开发,左滑删除

作者: 竹小星 | 来源:发表于2019-06-11 09:52 被阅读0次

    声明:

    1. 根据资料配合项目需求进行了一点修改,原文地址

    展示图例

    页面部分

    页面中‘list’部分定位在上层,滑动上层漏出下层右侧的‘删除’按钮,通过循环‘arr’,添加txtStyle样式,改变滑动状态

    <view class='box' wx:for="{{arr}}" wx:key='*this'>
      <view class='list'
        bindtouchstart="touchS" 
        bindtouchmove="touchM" 
        bindtouchend="touchE" 
        style="{{item.txtStyle}}" 
        data-index="{{index}}">
        {{item.title}}
      </view>
      <view class='btn'  
        catchtap="delOrder" 
        data-index='{{index}}' 
        data-id='{{item.id}}'>
        删除   
      </view>
    </view>
    

    JS部分

    ‘delBtnWidth’向左滑动的距离设定为删除按钮的宽度
    ‘openDelIdx’记录已经滑动过滑块的index,在滑动下一个条目或者点击删除按钮时还原之前滑动的样式

    Page({
      data: {
        arr: [],
        openDelIdx: 0, // 记录滑动过条目的index
        delBtnWidth: 150//删除按钮宽度单位(rpx)
      },
    
      /**
       * 还原之前滑动样式
       */
      clearLeftMove() {
        var list = this.data.arr;
        list[this.data.openDelIdx].txtStyle = 'left:0px'
        this.setData({
          arr: list
        });
      },
    
      /**
       * 手指触摸开始
       */
      touchS: function (e) {
        //判断是否只有一个触摸点
        if (e.touches.length == 1) {
          this.clearLeftMove();
          this.setData({
            //记录触摸起始位置的X坐标
            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:0";
          } else if (disX > 0) {//移动距离大于0,文本层left值等于手指移动距离
            txtStyle = "left:-" + disX + "rpx";
            if (disX >= delBtnWidth) {
              //控制手指移动距离最大值为删除按钮的宽度
              txtStyle = "left:-" + delBtnWidth + "rpx";
            }
          }
          //获取手指触摸的是哪一个item
          var index = e.currentTarget.dataset.index;
          var list = that.data.arr;
          //将拼接好的样式设置到当前item中
          list[index].txtStyle = txtStyle;
          //更新列表的状态
          this.setData({
            arr: list
          });
        }
      },
    
      /**
       * 手指触摸结束
       */
      touchE: function (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 + "rpx" : "left:0";
          //获取手指触摸的是哪一项
          var index = e.currentTarget.dataset.index;
          var list = that.data.arr;
          list[index].txtStyle = txtStyle;
          //更新列表的状态
          that.setData({
            openDelIdx: index,
            arr: list
          });
        }
      },
    
      /**
       * 删除订单
       */
      delOrder: function (e) {
        var order_id = e.currentTarget.dataset.id;
        var index = e.currentTarget.dataset.index;
        var that = this;
        that.clearLeftMove(); // 滑动恢复原状
        wx.showModal({
          title: '提示',
          content: '确认删除这个分类么?',
          success(res) {
            if (res.confirm) {    
              // 此处添加删除请求
              that.delItem(index);
            } else if (res.cancel) {
    
            }
          }
        })
        
      },
    
      /**
       * 删除页面item
       */
      delItem: function (index) {
        var list = this.data.arr
        list.splice(index, 1);
        this.setData({
          arr: list
        });
      },
    
      onLoad: function () {
        var list = [
          { id: 1, title: 'aaa' }, 
          { id: 2, title: 'bbb' }, 
          { id: 3, title: 'ccc' }, 
          { id: 4, title: 'ddd' }, 
          { id: 5, title: 'eee' }
        ];
        this.setData({
          arr: list
        });
      },
    })
    
    

    CSS部分

    .box{
      position: relative;
      border-bottom:1rpx solid #EAE8E8; 
      height: 118rpx;
      display: flex;
      flex-direction: row;
      justify-content: flex-end;
    }
    .list{
      padding: 16rpx 28rpx;
      font-size: 32rpx;
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      line-height: 86rpx;
      transition: left 0.2s ease-in-out;
      z-index: 5;
      background-color: #fff;
      box-sizing:border-box;
    }
    .btn{
      width: 150rpx;
      height: 100%;
      line-height: 118rpx;
      background-color: #FF5555;
      color: #fff;
      font-size: 32rpx;
      text-align: center;
    }
    

    最后附上代码片段

    https://developers.weixin.qq.com/s/hWvm12mM7H9j

    相关文章

      网友评论

          本文标题:微信小程序开发,左滑删除

          本文链接:https://www.haomeiwen.com/subject/jqezxctx.html