wxml结构(删除部分代码):

<view class="chapter-item" wx:for="{{klgData}}" data-index="{{index}}" bind:touchstart="touchS" bind:touchmove="touchM" bind:touchend="touchE" wx:key="item.id"><!-- 主要代码 -->
<view class="klg-content" style="{{item.contentStyle}}"><!-- 主要代码 -->
<view class="left-side">
<image wx:if="{{item.image}}" mode="aspectFill" class="chapter-image" src="{{host+item.image}}" />
</view>
<view class="right-side">
<text class="chapter-text chapter-name">{{item.title}}</text>
<text class="chapter-text chapter-time">时长:{{item.duration}}</text>
</view>
</view>
<view class="operates" style="{{item.btnStyle}}"><!-- 主要代码 -->
<view class="klg-btn returnBtn" catchtap="removeKlg">
<image mode="aspectFill" data-id="{{item._id}}" data-klgid="{{item.klgid}}" class="btn-icon" src="../../images/video_icon_value.png" />
撕书
</view>
<view class="klg-btn shareBtn"><image data-id="{{item.id}}" mode="aspectFill" class="btn-icon" src="../../images/video_point_icon_share.png" />分享</view>
</view>
</view>

wxss相关代码(主要是定位,已标识主要代码):

.chapter-item{
overflow:hidden;
border-bottom: 1px solid #eee;
position: relative;
height:80px;
display: flex;
}
.left-side,.right-side{
float:left;
display: inline-block;position:relative;height:58px;
}
.right-side{flex:;}
.left-side{width:105px;padding-right:10px;flex: 105px}
.chapter-name{flex:;display:block;color:#2E3330;}
.chapter-time{display:block;position:absolute;bottom:;left:;}
.chapter-image{width:104px;height:58px;}
.chapter-text{display: block;}
.klg-content{position: absolute;left:12px;top:10px;}//主要代码 绝对定位
.operates{position: absolute;top:;overflow:hidden;z-index: ;right:-160px;}//主要代码 绝对定位
.klg-btn{color:#34BC67;width:80px;line-height: 60px; float: left;text-align: center;font-size: 16px;position: relative;padding-top:24px;} 
.btn-icon{width:28px;height:28px;text-align: center;position: absolute;top:14px;left:25px;}

js相关代码:

//这里delBtnWidth为160,存放在data里面
//手指刚放到屏幕触发
touchS:function(e){
console.log("touchS"+e);
//判断是否只有一个触摸点
if(e.touches.length==){
this.setData({
//记录触摸起始位置的X坐标
startX:e.touches[].clientX
});
}
},
//触摸时触发,手指在屏幕上每移动一次,触发一次
touchM:function(e){
console.log("touchM:"+e);
var that = this
if(e.touches.length==){
//记录触摸点位置的X坐标
var moveX = e.touches[].clientX;
//计算手指起始点的X坐标与当前触摸点的X坐标的差值
var disX = that.data.startX - moveX;
//delBtnWidth 为右侧按钮区域的宽度
var delBtnWidth = that.data.delBtnWidth;
var contentStyle = "";
var btnStyle="";
if(disX == || disX < ){//如果移动距离小于等于0,文本层位置不变
contentStyle = "left:12px";
btnStyle = "right:-160px";
}else if(disX > ){//移动距离大于0,文本层left值等于手指移动距离
contentStyle = "left:-"+disX+"px";
btnStyle = "right:"+(-+disX)+"px";
if(disX>=delBtnWidth){
//控制手指移动距离最大值为删除按钮的宽度
contentStyle = "left:-"+delBtnWidth+"px";
btnStyle = "right:0px";
}
}
//获取手指触摸的是哪一个item
var index = e.currentTarget.dataset.index;
var list = that.data.klgData;
//将拼接好的样式设置到当前item中
list[index].contentStyle = contentStyle;
list[index].btnStyle = btnStyle;
//更新列表的状态
this.setData({
klgData:list
});
}
},
touchE:function(e){
console.log("touchE"+e);
var that = this
if(e.changedTouches.length==){
//手指移动结束后触摸点位置的X坐标
var endX = e.changedTouches[].clientX;
//触摸开始与结束,手指移动的距离
var disX = that.data.startX - endX;
var delBtnWidth = that.data.delBtnWidth;
//如果距离小于按钮的1/2,不显示按钮
var contentStyle = disX > delBtnWidth/ ? "left:-"+delBtnWidth+"px":"left:12px";
var btnStyle = disX > delBtnWidth/ ? "right:0px":"right:-160px";
//获取手指触摸的是哪一项
var index = e.currentTarget.dataset.index;
var list = that.data.klgData;
list[index].contentStyle = contentStyle;
list[index].btnStyle = btnStyle;
//更新列表的状态
that.setData({
klgData:list
});
}
}

结果:

微信小程序左滑显示按钮demo的更多相关文章

  1. 运用wxs制作微信小程序左滑功能和跳转,性能更优越

    锲子 微信小程序自定义左滑功能加上跳转,换成以往,左滑功能的逻辑一般是在js中实现,但在拖动方面,性能并不是那么的流畅.如今,官方新扩展了一套脚本语言wxs,在IOS设备上运行,性能会比JS快2~20 ...

  2. 微信小程序左滑删除

    <view class="touch-item {{item.isTouchMove ? 'touch-move-active' : ''}}" data-index=&qu ...

  3. 微信小程序左滑删除功能

    效果图如下: wxml代码: <view class="container"> <view class="touch-item {{item.isTou ...

  4. 微信小程序左滑删除未操作有复位效果

    1.wxml <!--pages/test/test.wxml--> <view class="page"> <movable-area class= ...

  5. 微信小程序开发——点击按钮获取用户授权没反应或反应很慢的解决方法

    异常描述: 点击按钮获取用户手机号码,有的时候会出现点击无反应或很久之后才弹出用户授权获取手机号码的弹窗,这种情况下,也会出现点击穿透的问题(详见:微信小程序开发——连续快速点击按钮调用小程序api返 ...

  6. 微信小程序之最简单的Demo设计使用

    这个小Demo,代码量不多:导航样式.View.Text.点击.JS交互的使用,主要是理解每个后缀文件的功能,然后才能更好的使用开发.......(下面代码和源代码没差别,实在想要的请留言,谢谢... ...

  7. 微信小程序开发——点击按钮退出小程序的实现

    微信小程序官方是没有提供退出的API的,但是在navigator这个组件中,是有退出这个功能的:详情参考官方文档:navigator.示例代码:1 navigator open-type=" ...

  8. 微信小程序页面内转发 按钮 转发

    通过给 button 组件设置属性 open-type="share",可以在用户点击按钮后触发 Page.onShareAppMessage() 事件,如果当前页面没有定义此事件 ...

  9. 微信小程序-隐藏和显示自定义的导航

    微信小程序中不能直接操作window对象,document文档,跟html的树结构不相同. 实现类似导航的隐藏显示,如图效果: 点击网络显示或隐藏网络中包含的内容.其他类似. 如果是jquery很方便 ...

随机推荐

  1. Windows 的 80 端口被 System 进程占用解决方案

    通过 Windows 的资源监视器(win+R:resmon)可以看到 80 端口已经被占用,下图是已经解决好了,没能截图被占用的情况,下面给出解决方案. PS:贴出两个好用的 windows cmd ...

  2. Size Balanced Tree(节点大小平衡树)

    定义 SBT也是一种自平衡二叉查找树,它的平衡原理是每棵树的大小不小于其兄弟树的子树的大小 即size(x->l)$\ge$size(x->r->l),size(x->r-&g ...

  3. curl下载安装与使用

    下载: https://curl.haxx.se/download.html 安装: 二进制安装.即解压即可. 使用 1.获取页面内容. 不加任何参数时,默认会发送GET请求来获取url内容到标准输出 ...

  4. AC自动机(模板) LUOGU P3808

    传送门 解题思路 AC自动机,是解决多模匹配问题的算法,是字典树与kmp结合的算法,可以解决许多子串在文本串中出现的次数等信息.关键是实现一个fail指针,是指向更靠上的前缀相同字母,从而可以实现在文 ...

  5. 使用innerHTML属性向head中插入字符时报“无法设置 innerHTML 属性。 该操作的目标元件无效”的错误

    向head中动态插入script文件,代码如下: var sc = document.createElement("script"); sc.src = "//www.c ...

  6. CodeChef--Cards, bags and coins

    题目链接 Yet another game from chef. Chef gives you N cards and M bags. Each of the N cards has an integ ...

  7. C++ string(STL)

    发现字符串问题中 string 好厉害- string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c ...

  8. Web前端开发的就业前景怎么样,薪资待遇如何

    信息技术的迅速发展,使IT技术者们赶上了一个百年难遇的好机会,尤其是国家出台了“互联网+”的政策后,更是催生了IT行业的就业空间,使其呈现爆发性增长. 如今,微信逐渐成为了大家主要的交流工具,随着各种 ...

  9. Neo4j属性图模型简单介绍

    本文主要是对Neo4j属性图模型简单的介绍. Neo4j是什么? Neo4j是一款是由java语言实现的图数据库,图形数据库将数据以图的数据结构进行存储和管理,并且能够以高度可问的方式优雅地表示任何种 ...

  10. python无法启动火狐浏览器且报错“selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities”

    安装了python2,使用pip安装了selenium,但是在使用时,报了“selenium.common.exceptions.WebDriverException: Message: 'gecko ...