微信小程序左滑删除
// 放在.wxml文件中
<view class="classAddress">
<view class="touch-item {{item.isTouchMove ? 'touch-move-active' : ''}}" data-index="{{index}}" wx:for="{{address_list}}" wx:key="user_id" bindtouchstart="touchstart" bindtouchmove="touchmove" bindtouchend="touchE">
<view class="content">
<view style="{{item.txtStyle}}" class='item' data-index='{{index}}'>
<view class="item_logo">
<view class='title username'>{{item.consignee}}</view>
</view>
<view class="item_container">
<text class="item_title" name="phone">{{item.mobile}}</text>
<text class="item_message">{{item.address}}</text>
</view>
<navigator class="item_time iconfont icon-bianji"></navigator>
</view>
</view>
<view class="del" data-id="{{item.address_id}}" catchtap="delBtn" data-index="{{index}}">删除</view>
</view>
<view class="bottom">
<navigator url="/pages/addaddr/addaddr" class="addBord">
<view>+新加地址</view>
</navigator>
</view>
</view>
// 样式
page {
width: 100%;
height: 100%;
}
.wx_list{
border-top:1rpx solid #f7f3f3;
box-sizing: border-box;
}
.wx_list:last-child{
border-bottom:1rpx solid #e0e0e0;
}
.item{
overflow: hidden;
/*border-bottom:1rpx solid #e0e0e0;*/
padding: 7px 13px;
display: flex;
}
.item_logo{
width: 30%;
height:100%;
}
.username {
width: 100%;
font-size: 25rpx;
display: -webkit-box;
word-break: break-all;
overflow: hidden;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1; /*设置超出两行进行隐藏*/
text-overflow: ellipsis;
padding-top: 4rpx;
}
.item_container{
height:44px;
overflow: hidden;
margin-left:10px;
width: 100%;
}
.item_title{
display: block;
margin-left:5px;
height:22px;
line-height:22px;
overflow: hidden;
white-space:nowrap;
text-overflow:ellipsis;
font-size: 14px;
}
.item_message{
display: block;
margin-left:5px;
height:22px;
line-height:22px;
overflow: hidden;
text-overflow:ellipsis;
color: #999;
font-size: 12px;
}
.item_time{
width: 6em;
text-align: center;
font-size: 14px;
line-height: 66rpx;
color: #999;
}
.addBord {
width: 95%;
height: 100%;
color: white;
background-color: red;
text-align: center;
padding: 20rpx;
border-radius: 10rpx;
}
.bottom {
width: 100%;
bottom: 1px;
position: fixed;
}
.touch-item {
display: flex;
justify-content: space-between; width: 100%;
overflow: hidden;
margin-bottom: 10rpx;
background: #f5f5f5;
height: 216rpx;
}
.content {
width: 100%;
margin-right: 0;
-webkit-transition: all 0.4s;
transition: all 0.4s;
-webkit-transform: translateX(90px);
transform: translateX(90px);
margin-left: -90px;
}
.del {
width: 90px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: #fff;
background-color: red;
-webkit-transform: translateX(90px);
transform: translateX(90px);
-webkit-transition: all 0.4s;
transition: all 0.4s;
font-size: 35rpx;
}
.touch-move-active .content, .touch-move-active .del {
-webkit-transform: translateX(0);
transform: translateX(0);
}
.classAddress {
width: 100%;
height: 100%;
}
// 放到.js中
//获取应用实例
const util = require('../../utils/util.js');
var app = getApp();
Page({
data : {
address_list:[],
totalPage:1,
page:1,
size:6,
fromPage:'',
cart_id:0,
startX: 0, //开始坐标
startY: 0,
page: 1,
},
touchE: function (e) {
// console.log(e);
var that = this
if (e.changedTouches.length == 1) {
//手指移动结束后触摸点位置的X坐标
var endX = e.changedTouches[0].clientX;
//触摸开始与结束,手指移动的距离
var disX = that.data.startX - endX;
var delBtnWidth = that.data.delBtnWidth;
//如果距离小于删除按钮的1/2,不显示删除按钮
var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "rpx" : "left:0rpx";
//获取手指触摸的是哪一项
var index = e.currentTarget.dataset.index;
var address_list = that.data.address_list;
address_list[index].txtStyle = txtStyle;
//更新列表的状态
that.setData({
address_list: address_list
});
}
},
//手指触摸动作开始 记录起点X坐标
touchstart: function (e) {
//开始触摸时 重置所有删除
this.data.address_list.forEach(function (v, i) {
if (v.isTouchMove) //只操作为true的
v.isTouchMove = false;
})
this.setData({
startX: e.changedTouches[0].clientX,
startY: e.changedTouches[0].clientY,
address_list: this.data.address_list
})
},
//滑动事件处理
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.address_list.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({
address_list: that.data.address_list
})
},
/**
* 计算滑动角度
* @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);
},
addAddress: function() {
wx.navigateTo({
url: '/pages/addaddr/addaddr',
})
},
onLoad: function (options) {
this.setData({
user_id: options.id,
fromPage: options.fromPage ? options.fromPage : "",
cart_id: options.cart_id ? options.cart_id : 0
});
},
onShow(){
var that = this;
that.setData({
address_list:[]
});
this.addressList();
},
addressList(){
var that = this;
var address_list = that.data.address_list;
var url = 'http://www.lgxj.com/index.php?g=Wxapp&m=Index&a=user_address_list';
var params = { user_id: 1,page:that.data.page,size:that.data.size};
wx.request({
url: url,
header: { 'content-type':'application/x-www-form-urlencoded'},
data: params || {},
method: 'POST',
success: function (res) {
if (res.statusCode == 200){
var list = res.data.data.res;
var newlist = util.arrayMerge(address_list, list);
that.setData({
address_list: newlist,
totalPage: res.data.data.totalPage
});
wx.stopPullDownRefresh();
}
},
})
},
delBtn: function (event) {
var that = this;
var address_id = event.currentTarget.dataset.id;
var url = 'http://www.lgxj.com/index.php?g=Wxapp&m=Index&a=user_address_del';
var params = { user_id: 1, address_id: address_id};
wx.request({
url: url,
data: params || {},
header: {'content-type': 'application/x-www-form-urlencoded'},
method: 'POST',
success: function (res) {
if(res.data.code == 1){
util.show('删除成功');
that.setData({
address_list : []
})
that.addressList();
}
},
});
}
});
// 下面的都放在公共文件中 util.js中
const arrayMerge = (arr0, arr1, type) => {
arr0 = typeof (arr0) == 'undefined' || arr0 == null || arr0 == undefined ? [] : arr0;
if (arr1 && typeof (arr1) == 'object') {
type = (typeof (type) == 'undefined' || type != 'before') ? 'after' : 'before';
for(var i in arr1){
if (type == 'after') {
arr0.push(arr1[i])
} else {
arr0.unshift(arr1[i])
}
}
}
return arr0;
}
const show = (title) => {
wx.showToast({
title: title,
duration: 1000,
mask: true
})
}
const refresh = () => {
wx.showLoading({
title: '刷新中',
icon: 'loading',
mask: true
})
}
const loading = () => {
wx.showLoading({
title: '加载中',
icon: 'loading',
duration: 1000,
mask: true
});
}
module.exports = {
show: show,
refresh: refresh,
loading: loading,
arrayMerge: arrayMerge
};