首页 > 热点 > 微信 > 正文

微信小程序实现收货地址左滑删除

2024-07-22 01:17:54
字体:
来源:转载
供稿:网友

本文实例为大家分享了微信小程序实现收货地址左滑删除的具体代码,供大家参考,具体内容如下

效果:

思路:

一、用相对定位和绝对定位,列表放在上层,删除按钮放在下层(z-index不要为负)。

二、触摸事件判断用户是否左滑,有 bindtouchstart,bindtouchmove,bindtouchend 三个触摸事件。

1、bindtouchstart 记录触摸开始的点。开始点的坐标在 e.touches[0] 中,这是相对于屏幕的,也就是以屏幕左上方为原点。

2、bindtouchmove 记录触摸移动时的点。同上。

3、bindtouchmove 记录触摸结束的点。结束点的坐标在 e.changedTouches[0] 中。

通过1、2方法,获取到触摸开始点、移动距离,就可以让列表层随触摸点左右移动;

通过3方法,获取最终点,判断与开始点的距离,如果这个距离小于删除按钮的一半,则还原列表层

代码:

1、wxml

<view wx:for="{{address}}" style='position: relative;'> <!-- 列表层 --> <view class='list' style='{{item.txtStyle}}' bindtouchstart="touchS" bindtouchmove="touchM" bindtouchend="touchE" data-index='{{index}}'> <!-- 收货信息 --> <view class='info' bindtap='select_addr' data-id="{{item.id}}">  <view>  {{item.name}}   <span class="phone">{{item.phone}}</span>  <span wx:if="{{item.default == 1}}" class='def'>默认</span>  </view>  <view>  {{item.province}} {{item.address}}  </view> </view> <!-- 编辑图标 --> <view class='edit' bindtap='edit' data-id='{{item.id}}' >  <image src='/image/edit.png'></image> </view> </view> <!-- 删除按钮 --> <view class="delete" data-id="{{item.id}}" data-index='{{index}}' bindtap="delItem" >删除</view></view> <view class='add' bindtap='add'>添加地址</view>

2、wxss

page{ background-color: #F0EFF5;}.list{ position: relative; z-index: 2; overflow: hidden; background-color: white; margin-top: 2rpx; padding: 25rpx; display: flex; align-items: center; justify-content:space-between; min-height: 150rpx;}.delete{ position: absolute; top:0rpx; background-color: #e64340; width: 180rpx; text-align: center; z-index: 1; right: 0; color: #fff; height: 100%; display: flex; align-items: center; justify-content: center;}.info{ display: flex; flex-direction: column; align-items: flex-start;}.info view:first-child{ text-align: center; font-size: 35rpx; margin-bottom: 10rpx;}.info view:nth-child(2){ font-size: 30rpx; margin-bottom: 10rpx;}.def{ font-size: 30rpx; border:1rpx solid red; border-radius: 5rpx; padding:0 10rpx; color: red; margin-right: 10rpx;}.phone{ color:gray;font-size:30rpx;margin: 0 20rpx;}.edit{ padding:40rpx;}.edit image{ width: 40rpx; height: 40rpx; margin-left:10rpx;}.add{ width: 650rpx; border: 2rpx solid gray; height: 100rpx; line-height: 100rpx; text-align: center; font-size: 30rpx; border-radius: 10rpx; position: fixed; bottom: 50rpx; left: 50rpx; background-color: white;}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表