微信小程序-收货地址左滑删除
我参照了其中的部分代码,如:bindtouchstart,bindtouchmove,bindtouchend事件多数组中偏移值的更改, 在结合微信 movable-area 和 movable-view 组件,使左滑后未操作,再左滑另一个元素时,有了恢复初始位置的效果.
效果在这里 : https://www.cnblogs.com/fps2tao/p/11390024.html
下面文章不是,本图效果....
思路:
一、用相对定位和绝对定位,列表放在上层,删除按钮放在下层(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:;
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:;
right:;
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;
}
3、JS
Page({
data: {
address:[
{
id: "1",
address: "1单元222号",
name: "啦啦啦",
default:"1",
phone: "12222223333",
province: "河北省 石家庄市 长安区",
txtStyle: "",
},
{
id: "2",
address: "2幢2楼222号",
name: "嚯嚯嚯",
default: "0",
phone: "12345678900",
province: "浙江省 杭州市 市辖区",
txtStyle: "",
},
{
id: "3",
address: "1幢1单元",
name: "哈哈哈",
default: "0",
phone: "18208350499",
province: "河北省 石家庄市 新华区",
txtStyle: "",
}
],
delBtnWidth: 180
}, onLoad: function (options) {
//获取收货地址 省略
}, edit: function (e) {
//编辑收货地址 省略
}, add: function () {
//增加收货地址 省略
}, delItem: function (e) {
var id = e.currentTarget.dataset.id;
var index = e.currentTarget.dataset.index;
this.data.address.splice(index, 1);
this.setData({
address: this.data.address
})
}, touchS: function (e) {
if (e.touches.length == 1) {
this.setData({
//设置触摸起始点水平方向位置
startX: e.touches[0].clientX
});
}
}, touchM: function (e) {
if (e.touches.length == 1) {
//手指移动时水平方向位置
var moveX = e.touches[0].clientX;
//手指起始点位置与移动期间的差值
var disX = this.data.startX - moveX;
var delBtnWidth = this.data.delBtnWidth;
var txtStyle = "";
if (disX == 0 || disX < 0) {//如果移动距离小于等于0,文本层位置不变
txtStyle = "left:0rpx";
} else if (disX > 0) {//移动距离大于0,文本层left值等于手指移动距离
txtStyle = "left:-" + disX + "rpx";
if (disX >= delBtnWidth) {
//控制手指移动距离最大值为删除按钮的宽度
txtStyle = "left:-" + delBtnWidth + "rpx";
}
}
//获取手指触摸的是哪一项
var index = e.currentTarget.dataset.index;
var list = this.data.address;
list[index]['txtStyle'] = txtStyle;
//更新列表的状态
this.setData({
address: list
});
}
},
touchE: function (e) {
if (e.changedTouches.length == 1) {
//手指移动结束后水平位置
var endX = e.changedTouches[0].clientX;
//触摸开始与结束,手指移动的距离
var disX = this.data.startX - endX;
var delBtnWidth = this.data.delBtnWidth;
//如果距离小于删除按钮的1/2,不显示删除按钮
var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "rpx" : "left:0rpx";
//获取手指触摸的是哪一项
var index = e.currentTarget.dataset.index;
var list = this.data.address;
var del_index = '';
disX > delBtnWidth / 2 ? del_index = index : del_index = '';
list[index].txtStyle = txtStyle;
//更新列表的状态
this.setData({
address: list,
del_index: del_index
});
}
},
})
转: https://blog.csdn.net/qq_33514421/article/details/82497246
微信小程序-收货地址左滑删除的更多相关文章
- JavaScript和微信小程序获取IP地址的方法
最近公司新加了一个需求,根据用户登录的IP地址判断是否重复登录,重复登录就进行逼退,那么怎么获取到浏览器的IP地址呢?最后发现搜狐提供了一个JS接口,可以通过它获取到客户端的IP. 接口地址如下: h ...
- 微信小程序开发-IP地址查询-例子
微信小程序开发 小程序搜索框 IP地址查询 搜索查询 样例 微信小程序 开发 参考 https://mp.weixin.qq.com/debug/wxadoc/dev/component/ ...
- ecshop 订单-》设置默认收货地址,或者删除
设置位置:ecs_users标的 country字段,默认是0,默认地址是 users_address 的address_id 设置默认收货地址 /** * 设置默认地址 * * @access ...
- 微信小程序获取当前地址以及选择地址详解 地点标记
首先定义事件: bindtap='getLocation' <view class='store-bot' bindtap='getLocation'> <view class='c ...
- 转载:第五弹!全球首个微信小程序(应用号)开发教程!通宵吐血赶稿,每日更新!
博卡君今天继续更新,忙了一天,终于有时间开工写教程.不罗嗦了,今天我们来看看如何实现一些前端的功能和效果. 第八章:微信小程序分组开发与左滑功能实现 先来看看今天的整体思路: 进入分组管理页面--&g ...
- 微信小程序实例教程(四)
第八章:微信小程序分组开发与左滑功能实现 先来看看今天的整体思路: 进入分组管理页面 --> 点击新建分组新建 进入到未分组页面基本操作 进入到已建分组里面底部菜单栏操作 --> 从名 ...
- 微信小程序开发学习资料
作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- 微信小程序——编辑
记录一下 微信小程序分页编辑,可增页删除当前页面.第一页为主图片和主句子.其他页面一致. 左滑右滑可切换页面.每页可增加0到1页.小黑点与页面一致. /* pages/booktool/write/w ...
- 微信小程序demo
微信小程序demo github地址 去年小程序刚发布时特别火,赶潮流做了个demo.感觉小程序开发还是比较简单的,主要是官方文档写得比较好,遗憾的是很多API需要微信认证才能使用. 由于小程序包大小 ...
随机推荐
- Android笔记(七十二) Style和Theme
我们尝尝需要使用setText.setColor.setTextSize等属性来设置控件的样式,但是每个控件都需要设置这些属性,工作量无疑是巨大的,并且后期维护起来也不方便. Style Androi ...
- Android笔记(六十二)网络框架volley
什么是Volley 很多时候,我们的APP都需要用到网络技术,使用HTTP协议来发送接收数据,谷歌推出了一个网络框架——volley,该框架适合进行数据量不大,但通信频繁的网络操作. 它的优点: (1 ...
- 【HICP Gauss】数据库 数据库管理(调优 启动流程)-4
数据库参数: 创建数据库 调优数据库 其他---->控制资源使用 控制数据库内部机制 设置重要属性 参数数据保存在cfg/Zengine.ini 文件中 参数保存使用 key=value的保存形 ...
- typescript 参数类型
1.参数类型:在参数名称后面使用冒号来指定参数的类型 var myname:string = 'wzn' => "use strict"; var myname = 'wzn ...
- Python 利用random库来实现圆周率的运算
蒙特卡罗方法求解圆周率 随机向一个正方形以及其内切圆(半径为1)的图形中随机抛洒大量的点,计算每个点到圆心的距离从而判断该点在圆内或圆外,用圆内的点除以总点数就是π/4的值.点数越多,值就越精确. 具 ...
- JS判断移动端访问设备并加载对应CSS样式
JS判断不同web访问环境,主要针对移动设备,提供相对应的解析方案(判断设备代码直接copy腾讯网的) // 判断是否为移动端运行环境 if(/AppleWebKit.*Mobile/i.test(n ...
- hibernate之关联关系一对多
什么是关联(association) 关联指的是类之间的引用关系.如果类A与类B关联,那么被引用的类B将被定义为类A的属性.例如: public class B{ private St ...
- django 第三天 视图
今日内容 一.url路由分发之include 项目文件夹下的urls.py文件中的url写法: from django.conf.urls import url,include from django ...
- 数据库基准测试标准 TPC-C or TPC-H or TPC-DS
针对数据库不同的使用场景TPC组织发布了多项测试标准.其中被业界广泛接受和使用的有TPC-C .TPC-H和TPC-DS. TPC-C: Approved in July of 1992, TPC B ...
- YAML_04 用user模块添加用户,并修改密码
ansible]# vim user.yml --- - hosts: cache remote_user: root vars: username: lisi tasks: ...