微信小程序左滑动删除效果

最近做微信小程序项目遇到左滑动效果,比如在我的收藏页面,我的历史浏览记录页面,我的购物车页面,大多数都会用到这种效果,我把代码复制出来,供大家参考,用的时候直接用模板,再此基础上修改就行。
wxml中的代码:

  <view class="touch-item {{item.isTouchMove ? 'touch-move-active' : ''}}" data-index="  {{index}}" bindtouchstart="touchstart" bindtouchmove="touchmove" wx:for="{{items}}" wx:key="">
    <view class="content">
      <view class='com'>
        <view class='tp'>
          <image src="{{item.image_src}}" class='img' />
        </view>
        <view class='txt'>
          <view class='tit'>{{item.goods_name}}</view>

          <view class='bot'>

            <block wx:if="{{item.marketable=='true'}}">
              <view class="pri">
                <text class="new-price">¥{{item.goods_price}}</text>
                <text class="old-price">¥{{item.mktprice}}</text>
              </view>
              <navigator class='a'>
                <label class='ti1'>可使用优惠券</label>
              </navigator>
            </block>
            <block wx:else>
              <navigator class='a'>
                <label class='ti'>对不起该商品已下架</label>
              </navigator>
            </block>

          </view>
        </view>
      </view>
    </view>
    <view class="del" catchtap="del" data-index="{{index}}" data-productid="{{item.product_id}}" data-goodsid="{{item.goods_id}}">删除</view>
  </view>

我这是对完接口之后的代码,循环items,然后用{{item.}}取到下面的值,并且用了一个判断,如果后台返回来的字段值marketable==‘true’,让其显示商品的销售价和原价,否则显示该商品已下架。
js中的代码:

 data: {
     startX: 0, //开始坐标
     startY: 0
  },
    touchstart: function(e) {
    //开始触摸时 重置所有删除
    this.data.items.forEach(function(v, i) {
      if (v.isTouchMove) //只操作为true的
        v.isTouchMove = false;
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      items: this.data.items
    })
  },
  //滑动事件处理
  touchmove: function(e) {
    var that = this,
      index = e.currentTarget.dataset.index, //当前索引
      startX = that.data.startX, //开始X坐标
      startY = that.data.startY, //开始Y坐标
      touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
      //获取滑动角度
      angle = that.angle({
        X: startX,
        Y: startY
      }, {
        X: touchMoveX,
        Y: touchMoveY
      });
    that.data.items.forEach(function(v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX) //右滑
          v.isTouchMove = false
        else //左滑
          v.isTouchMove = true
      }
    })
    //更新数据
    that.setData({
      items: that.data.items
    })
  },

  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  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);
  },

wxml中的代码:

.touch-item {
  background-color: #fff;
  margin-top: 20rpx;
  display: flex;
  justify-content: space-between;
  width: 100%;
  overflow: hidden;
  box-sizing: border-box;
}

.content {
  width: 100%;
  -webkit-transition: all 0.4s;
  transition: all 0.4s;
  -webkit-transform: translateX(180rpx);
  transform: translateX(180rpx);
  margin-left: -180rpx;
}

.touch-move-active .content, .touch-move-active .del {
  -webkit-transform: translateX(0);
  transform: translateX(0);
}

.com {
  padding: 25rpx;
  height: 210rpx;
}

.tp {
  background-color: white;
  float: left;
}

.img {
  width: 210rpx;
  height: 210rpx;
  display: inline-block;
  vertical-align: middle;
  border-radius: 15rpx;
}

.txt {
  width: 420rpx;
  margin-left: 270rpx;
  position: relative;
}

.txt .tit {
  font-size: 28rpx;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.txt .bot {
  width: 100%;
  font-size: 24rpx;
  margin-top: 20rpx;
}

.ti1 {
  margin-top: 15rpx;
  font-size: 23rpx;
  background-color: #fce64c;
  padding: 10rpx;
  display: inline-block;
}

.ti {
  margin-top: 35rpx;
  font-size: 28rpx;
  display: inline-block;
  color: #a2a2a2;
}

.del {
  background-color: red;
  width: 180rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #fff;
  -webkit-transform: translateX(180rpx);
  transform: translateX(180rpx);
  -webkit-transition: all 0.4s;
  transition: all 0.4s;
}

.new-price {
  font-weight: 600;
  color: #ff503c;
  font-size: 35rpx;
}

.old-price {
  margin-left: 30rpx;
  text-decoration: line-through;
  font-size: 28rpx;
  color: #b1b1b1;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以在微信小程序中使用 `<swiper>` 组件来实现向左滑动显示删除或取消的效果。具体实现方式如下: 1. 在页面的 `.wxml` 文件中添加 `<swiper>` 组件,设置 `class` 和 `duration` 属性: ```html <swiper class="swiper-item" duration="{{animationDuration}}" bindchange="swiperChange"> <view class="swiper-item-content">{{item.content}}</view> <view class="swiper-item-delete" bindtap="deleteItem">删除</view> </swiper> ``` 2. 在页面的 `.wxss` 文件中设置 `.swiper-item` 和 `.swiper-item-delete` 的样式: ```css .swiper-item { height: 60px; width: 100%; display: flex; align-items: center; } .swiper-item-content { flex: 1; padding-left: 20px; } .swiper-item-delete { width: 80px; height: 60px; background-color: red; color: #fff; display: flex; justify-content: center; align-items: center; } ``` 3. 在页面的 `.js` 文件中设置 `animationDuration`、`swiperChange` 和 `deleteItem` 函数,实现滑动删除效果: ```javascript Page({ data: { items: [ {id: 1, content: 'item 1'}, {id: 2, content: 'item 2'}, {id: 3, content: 'item 3'}, {id: 4, content: 'item 4'}, {id: 5, content: 'item 5'} ], animationDuration: 500 }, swiperChange: function(e) { if (e.detail.source == 'touch') { var animation = wx.createAnimation({ duration: this.data.animationDuration }) animation.translateX(-80).step() var index = e.currentTarget.dataset.index this.setData({ ['items[' + index + '].animation']: animation.export() }) } }, deleteItem: function(e) { var index = e.currentTarget.dataset.index this.data.items.splice(index, 1) this.setData({ items: this.data.items }) } }) ``` 这样就可以实现向左滑动显示删除或取消的效果了。当用户向左滑动 `<swiper>` 组件时,会触发 `swiperChange` 函数,通过 `createAnimation` 创建动画实现向平移的效果,并将动画绑定到对应的数据项上。当用户点击删除按钮时,会触发 `deleteItem` 函数,从数据中删除对应的项,实现删除效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值