微信小程序实现滑动删除效果
作者:东边的小山 时间:2024-04-19 10:03:45
最初打算使用scroll-view实现,效果好、流畅、有惯性滑动,但由于滚动条没法去掉、无法实现上下层的帧布局,最终放弃了。
还是自己写个吧,利用手势事件。遗憾的是小程序中目前没有像Android中快速滑动事件,所以,要实现惯性滑动是不可能了。
item的布局:
推荐小程序优先使用flex布局,完全够用。这也是微信推荐的。
<view wx:for="{{cardTeams}}" wx:for-item="cardTeam" id="{{cardTeam.id}}" class="item" bindtouchstart="drawStart" bindtouchmove="drawMove" bindtouchend="drawEnd" style="right:{{cardTeam.right}}px">
<image class="img" src="http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg" mode="scaleToFill"></image>
<view class="number-wrapper">
<text class="name">{{cardTeam.name}}</text>
<view class="count-wrapper">
<view class="decrease-btn">-</view>
<text class="count">1</text>
<view class="increase-btn">+</view>
</view>
<view class="price-wrapper">
<text class="unit">¥</text>
<text class="price">99.80</text>
</view>
</view>
<view class="ok"><view class="inner-ok">确定</view></view>
<view class="remove" data-id="{{cardTeam.id}}" bindtap="delItem"><view>删除</view></view>
</view>
主要是将删除按钮的设为绝对定位(position: absolute):
.item .remove{
width: 60px;
height: 100%;
background-color: red;
position: absolute;
top: 0;
right: -60px;
display: flex;
justify-content: center;
align-items: center;
}
Page({
data: {
cardTeams:[{"id":"aaaaa", "name":"android教程", "url":"http://www.see-source.com", "right":0, "startRight":0},{"id":"bbbb", "name":"小程序教程", "url":"http://www.see-source.com", "right":0, "startRight":0}]
},
drawStart : function(e){
// console.log("drawStart");
var touch = e.touches[0];
startX = touch.clientX;
startY = touch.clientY;
var cardTeams = this.data.cardTeams;
for(var i in cardTeams){
var data = cardTeams[i];
data.startRight = data.right;
}
key = true;
},
drawEnd : function(e){
console.log("drawEnd");
var cardTeams = this.data.cardTeams;
for(var i in cardTeams){
var data = cardTeams[i];
if(data.right <= 100/2){
data.right = 0;
}else{
data.right = maxRight;
}
}
this.setData({
cardTeams:cardTeams
});
},
drawMove : function(e){
//console.log("drawMove");
var self = this;
var dataId = e.currentTarget.id;
var cardTeams = this.data.cardTeams;
if(key){
var touch = e.touches[0];
endX = touch.clientX;
endY = touch.clientY;
console.log("startX="+startX+" endX="+endX );
if(endX - startX == 0)
return ;
var res = cardTeams;
//从右往左
if((endX - startX) < 0){
for(var k in res){
var data = res[k];
if(res[k].id == dataId){
var startRight = res[k].startRight;
var change = startX - endX;
startRight += change;
if(startRight > maxRight)
startRight = maxRight;
res[k].right = startRight;
}
}
}else{//从左往右
for(var k in res){
var data = res[k];
if(res[k].id == dataId){
var startRight = res[k].startRight;
var change = endX - startX;
startRight -= change;
if(startRight < 0)
startRight = 0;
res[k].right = startRight ;
}
}
}
self.setData({
cardTeams:cardTeams
});
}
},
//删除item
delItem: function(e){
var dataId = e.target.dataset.id;
console.log("删除"+dataId);
var cardTeams = this.data.cardTeams;
var newCardTeams = [];
for(var i in cardTeams){
var item = cardTeams[i];
if(item.id != dataId){
newCardTeams.push(item);
}
}
this.setData({
cardTeams:newCardTeams
});
},
onLoad: function () {
console.log('onLoad:'+app.globalData.domain)
}
})
drawStart用于记录手指触碰时的位置,drawMove记录手指滑动的位置,两者的差值就是删除按钮的偏移量
drawEnd手指抬起时触发,用于设置弹回、摊开效果。目前我的是超过一半自动弹开,不足一半自动收回。
标签:微信小程序,滑动删除
0
投稿
猜你喜欢
Python中使用urllib2模块编写爬虫的简单上手示例
2023-10-18 22:14:31
Mysql数据库备份和还原常用的命令
2011-12-01 10:41:54
Web页面空间利用率的思考
2009-07-03 12:45:00
Python面向对象程序设计类的多态用法详解
2021-05-28 20:21:29
Python+OpenCV数字图像处理之ROI区域的提取
2021-01-17 18:54:43
php 时间计算问题小结
2023-11-03 14:37:06
tensorflow之自定义神经网络层实例
2022-05-26 07:05:27
十条建议帮你提高Python编程效率
2021-07-18 02:55:54
Golang中panic的异常处理
2023-10-13 12:27:23
使用Python的networkx绘制精美网络图教程
2022-10-31 06:52:47
如何把Oracle 数据库从 RAC 集群迁移到单机环境
2024-01-28 06:29:05
详解Vue.use自定义自己的全局组件
2024-05-02 17:02:59
bootstrap改变按钮加载状态
2023-07-02 05:24:48
Oracle中获取执行计划的几种方法分析
2023-07-17 15:18:31
一个简单的北京2008奥运倒计时代码
2008-03-16 14:15:00
Windows下在CMD下执行Go出现中文乱码的解决方法
2024-04-25 15:17:27
解决PHP 7编译安装错误:cannot stat ‘phar.phar’: No such file or directory
2023-09-05 06:07:44
Go http client 连接池不复用的问题
2024-02-03 05:17:15
SQL2005CLR函数扩展-数据导出的实现详解
2024-01-25 11:59:57
Python实现获取网页内容及自动填表单与登录功能
2023-10-23 23:55:27