微信小程序 uni-app 滑动出现删除

<view
  :class="'list_item ' + (item.touch_move ? 'touch_move' : '')"
  @touchstart="touchstart"
  @touchmove="touchmove"
  :data-index="index"
>
<navigator class="list_item_box">
内容
</navigator>
<view
  class="list_item_del"
  @tap="list_item_del"
  :data-id="item.id"
  v-if="type == 1"
  >删除</view
>
</view>
.list_item {
  display: flex;
  justify-content: space-between;
}
.list_item.touch_move .list_item_del,
.list_item.touch_move .list_item_box {
  transform: translateX(0);
}
.list_item view {
  flex-shrink: 0;
}
.list_item_del {
  display: none;
  width: 90px;
  color: #ffffff;
  background: #ff0000;
  transition: all 0.4s;
  transform: translateX(90px);
  border-top-right-radius: 5rpx;
  border-bottom-right-radius: 5rpx;
  overflow: hidden;
}
.list_item.touch_move .list_item_del {
  display: flex;
  align-items: center;
  justify-content: center;
}
.list_item_box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  margin-left: -90px;
  transition: all 0.4s;
  transform: translateX(90px);
}
touchstart: function (e) {
  var type = that.type;
  if (type == 1) {
    var list = that.list;
    list.forEach(function (v, i) {
      if (v.touch_move) {
        v.touch_move = false;
      }
    });
    that.list = list;
    that.start_x = e.changedTouches[0].clientX;
    that.start_y = e.changedTouches[0].clientY;
    that.$forceUpdate();
  }
},
touchmove: function (e) {
  var type = that.type;
  if (type == 1) {
    var index = e.currentTarget.dataset.index;
    var start_x = that.start_x;
    var start_y = that.start_y;
    var touchMoveX = e.changedTouches[0].clientX;
    var touchMoveY = e.changedTouches[0].clientY;
    var angle = that.angle(
      { X: start_x, Y: start_y },
      { X: touchMoveX, Y: touchMoveY }
    );
    var list = that.list;
    list.forEach(function (v, i) {
      v.touch_move = false;
      if (Math.abs(angle) > 30) {
        return;
      }
      if (i == index) {
        if (touchMoveX > start_x) {
          v.touch_move = false;
        } else {
          v.touch_move = true;
        }
      }
    });
    that.list = list;
    that.$forceUpdate();
  }
},
angle: function (start, end) {
  var _X = end.X - start.X,
    _Y = end.Y - start.Y;
  //返回角度 /Math.atan()返回数字的反正切值
  return (360 * Math.atan(_Y / _X)) / (2 * Math.PI);
},
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值