微信小程序 购物车 滑动删除


<view class="page">
  <movable-area class="item-touch" wx:for="{{cartdata}}" wx:index="idx" wx:key="index">  
    <movable-view bindtouchstart="_touchStart" bindtouchmove="_touchMove" bindtouchend="_touchEnd" x="{{item.x}}" direction="horizontal" inertia="true" out-of-bounds="true" data-i="{{index}}" class="item-touch-list flex" data-id="touch">
       {{index}}购物车商品{{item.id}}
    </movable-view>  
    <view class="item-touch-tool">删除</view>
  </movable-area>
</view>
.page {
  position: fixed;
  display: flex;
  flex-direction: column;
  background-color: #f5f5f5;
  width: 100%;
  height: 100%;
  border-top: 20rpx solid#f5f5f5
}

.item-touch {
  display: flex;
  position: relative;
  margin-bottom: 20rpx;
  width: calc(750rpx - 148rpx);
  height: 100rpx;
  line-height: 100rpx;
}

.item-touch-list {
  z-index: 10;
  width: 750rpx;
  height: 100%;
  padding-left: 30rpx;
  box-sizing: border-box;
  background-color: #fff;
}

.item-touch-tool {
  width: 148rpx;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 100%;
  background-color: #f43838;
  font-size: 28rpx;
  line-height: 100rpx;
  text-align: center;
  color: #fff;
}
//logs.js
const util = require('../../utils/util.js')

Page({
  data: {
    logs: [],
    cartdata: [{ id: 1, x: 0 }, { id: 2, x: 0 }, { id: 3, x: 0 }],
    startX:0,
    isLeft:0,
    delBtnw:148
  },
  _touchStart:function(e){
    console.log(e);
    //排除多点触摸
    if(e.touches.length==1){
      this.setData({
        startX:e.touches[0].clientX
      });
    }

    console.log('start'+util.formatTime(new Date()));
  },
  _touchMove:function(e){
 
  },
  _touchEnd:function(e){
    var that=this;
    console.log(e);
    var cartIndex=e.currentTarget.dataset.i;

    var delw=that.data.delBtnw;
    if(e.changedTouches.length==1){
      var endX = e.changedTouches[0].clientX-that.data.startX;
            // 判断是否向左滑动
      if (endX>=0){
        that.setData({
          isLeft:0
        })
      }else{
        that.setData({
          isLeft:1
        })
      }
      console.log(endX);
      if(Math.abs(endX)<delw/4)
      {
        that.xMove(cartIndex,0);
      }else{
        that.xMove(cartIndex,that.data.delBtnw);
      }
    }
  },
  xMove:function(i,x){
    console.log(i+','+x);
    var that=this;
    var tempData=that.data.cartdata;
    console.log(that.data.isLeft);
    if(that.data.isLeft){
      tempData[i].x = tempData[i].x - x;      
    }else
    {
      tempData[i].x = tempData[i].x + x;
    }

    that.setData({
      cartdata:tempData
    });
    console.log('xMove:');
    console.log(that.data.cartdata);
  },
  onLoad: function () {

  }
})

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值