当前位置:网站首页>小程序中自定义行内左滑按钮,类似于qq和wx消息界面那种
小程序中自定义行内左滑按钮,类似于qq和wx消息界面那种
2022-07-05 09:46:00 【huxiaoxiao.】
平常在使用QQ或者WX聊天,在消息列表中某一项左滑,会出现几个操作按钮,什么置顶啊,标记未读等等,下面用原生小程序实现一下这个功能
wxml部分
<!-- moveTarget 主要记录当前滑动或者关闭的状态 -->
<view class="list-group {
{moveTarget=='move-box-'+index?('item-move'+(btnList.length>3?3:btnList.length)):''}}"
mut-bind:touchmove="touchListMove"
mut-bind:touchstart="touchListStrat"
data-target="move-box-{
{index}}"
wx:for="{
{reportDataList}}" wx:key="index">
<view class="box">
{
{item}}
</view>
<!-- btnList,左滑后的按钮数组 -->
<view class="btn-group" wx:if="{
{btnList.length>0}}">
<view wx:for="{
{btnList}}" wx:key="index" class="btn" bindtap="tapBtn"
data-btnindex="{
{index}}">
{
{item}}
</view>
</view>
</view>
1.moveTarget
在标签中定义了 data-target="move-box-{ {index}}" , 比如页面中有十个盒子,你滑动了第一个,那么对应取到的就是 move-box-0, 然后在方法中给moveTarget赋值 data-target ,当二者相同时,对应的盒子才可以滑动,如果不做这一层限制,就会导致滑动一个盒子,所有盒子一起动
2. mut-bind:touchmove="touchListMove" mut-bind:touchstart="touchListStrat"
对应的滑动开始和滑动结束的方法:mut-bind ,我也不是很理解很详细的意思,大概就是一个方法生效另一个也跟着生效,感兴趣的可以百度一下
3.{ {moveTarget=='move-box-'+index?('item-move'+(btnList.length>3?3:btnList.length)):''}}
这里就是对应着 (1) 里面所说的,如果相等,找到对应的盒子,添加后面的类名,类名对应的样式中规定了向左滑动的宽度
wxss部分
/* 假设一个操作按钮的宽度为 166rpx,item-move1代表一个操作按钮的样式,依此类推 */
.list-group.item-move3{
transform: translateX(-500rpx);
}
.list-group.item-move2{
transform: translateX(-332rpx);
}
.list-group.item-move1{
transform: translateX(-166rpx);
}
.list-group {
position: relative;
height: 100rpx;
line-height: 100rpx;
width: 100%;
background-color: #f0f0f0;
margin-bottom: 10rpx;
transition: all .6s ease-in-out 0s;
}
/* 把操作按钮定位到盒子右侧,记得加 transform: translateX(100%); */
.btn-group {
position: absolute;
right: 0;
top: 0;
width: 500rpx;
height: 100%;
display: flex;
flex-direction: row;
transform: translateX(100%);
}
.btn-group .btn{
width: 166.5rpx;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
js部分
这里所有数据都是我自定义,实际可能后台获取数据
// pages/index/component/index.js
Component({
/**
* 页面的初始数据
*/
data: {
reportDataList: ['盒子1','盒子2'], // 页面盒子数组
btnList: ['按钮1','按钮2','按钮3'], // 操作按钮数组
moveStartX: 0, // 滑动距离
moveTarget: '', // 滑动控制
},
// 组件方法中心
methods: {
// 滑动开始
touchListStrat(e) {
this.setData({
moveStartX: e.touches[0].pageX
})
},
// 滑动结束
touchListMove(e) {
let moveEndX = e.touches[0].pageX;
let moveStartX = this.data.moveStartX;
let btnList = this.data.btnList;
if (btnList.length>0) {
let size = Math.abs(moveEndX - moveStartX);
if (moveEndX < moveStartX) {
if (size > 20) {
this.setData({
moveTarget: e.currentTarget.dataset.target
})
}
} else {
this.setData({
moveTarget: ''
})
}
}
},
// 点击行内按钮
tapBtn(e) {
let btnindex = e.currentTarget.dataset.btnindex;
// 在这里可以定义按钮点击事件
// 点击按钮时让moveTarget等于空,这样等于找不到当前你滑动的盒子,所以点击之后滑动就会恢复原样
this.setData({
moveTarget: ""
})
}
}
})
效果已经大致实现了
边栏推荐
- Cent7 Oracle database installation error
- StaticLayout的使用详解
- ArcGIS Pro creating features
- 基于单片机步进电机控制器设计(正转反转指示灯挡位)
- 学习笔记6--卫星定位技术(上)
- 卷起來,突破35歲焦慮,動畫演示CPU記錄函數調用過程
- Z-blog template installation and use tutorial
- [tips] get the x-axis and y-axis values of cdfplot function in MATLAB
- 宝塔面板MySQL无法启动
- 苹果 5G 芯片研发失败?想要摆脱高通为时过早
猜你喜欢
Energy momentum: how to achieve carbon neutralization in the power industry?
[tips] get the x-axis and y-axis values of cdfplot function in MATLAB
Cut off 20% of Imagenet data volume, and the performance of the model will not decline! Meta Stanford et al. Proposed a new method, using knowledge distillation to slim down the data set
Constrained layout flow
Design and exploration of Baidu comment Center
Apache dolphin scheduler system architecture design
Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常
How Windows bat script automatically executes sqlcipher command
Common fault analysis and Countermeasures of using MySQL in go language
StaticLayout的使用详解
随机推荐
橫向滾動的RecycleView一屏顯示五個半,低於五個平均分布
Wechat applet - simple diet recommendation (2)
【系统设计】指标监控和告警系统
Uni app running to wechat development tool cannot Preview
Dedecms website building tutorial
Tianlong Babu TLBB series - about items dropped from packages
自动化规范检查软件如何发展而来?
> Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair
. Net delay queue
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 - 上
The king of pirated Dall · e? 50000 images per day, crowded hugging face server, and openai ordered to change its name
Cut off 20% of Imagenet data volume, and the performance of the model will not decline! Meta Stanford et al. Proposed a new method, using knowledge distillation to slim down the data set
RMS TO EAP通过MQTT简单实现
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 -下
Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
历史上的今天:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生...
Roll up, break 35 - year - old Anxiety, animation Demonstration CPU recording Function call Process
程序员如何活成自己喜欢的模样?
To bring Euler's innovation to the world, SUSE should be the guide
ArcGIS Pro 创建要素