uniapp实现滑动删除

<template>
	<view>
		<view class="content">
			<view v-for="(item,index) in list">
				<movable-area class="m-area" v-if="item.choose_index==0">
				  <movable-view class="m-view" direction="horizontal" @change="onchange()" :x="c_index == index?juli:0">
				    <view class="left" @click="moveMview($event,index)">
						<image src="../../static/images/my/fankuimessage.png"></image>
						<view class="fankuicontent">
							<view class="text">
								{{item.content}}
							</view>
							<view class="date">
								{{item.date}}
							</view>
						</view>
					</view>
				    <view class="right">
				      <view class="delete" @click="shanchu(index)">删除</view>
				    </view>
				  </movable-view>
				</movable-area>
			</view>
		</view>
	</view>
</template>
page{
	background-color: #f6f6f6;
}
.content{
	width: 100%;
	height: 100%;
	box-sizing: border-box;
	padding-top: 20upx;
}
.m-area{
	background-color: #fff;
	width: 750upx;
	height: 150upx;
}
.m-view{
	width: 884upx;
	height: 150upx;
	display: flex;
}
.left{
	width: 100%;
	display: flex;
	padding: 30upx 39upx 34upx 40upx;
	box-sizing: border-box;
}
.left>image{
	width: 95upx;
	height: 85upx;
}
.fankuicontent{
	display: flex;
	width: 100%;
	flex-direction: column;
	margin-left: 21upx;
}
.text{
	font-size: 25upx;
	font-weight: 500;
	color: #333333;
}
.date{
	font-size: 20upx;
	margin-top: 10upx;
	color: #8C8C8C;
}
.right{
	display: flex;
}
.delete{
	background-color: #F01D1D;
	color: #fff;
	width: 134upx;
	display: flex;
	justify-content: center;
	align-items: center;
	font-size: 28upx;
	font-weight: bold;
}
<script>
	export default {
		data() {
			return {
				list:[
					{
						content:'有人举报偷盗我的视频,希望能查一下,账号是15788880000',date:'08:56',choose_index:0,x:0
					},
					{
						content:'我的提现失败了,是什么原因呢,请帮我查看一下, 联系方式:15788880000',date:'08:56',choose_index:0,x:0
					},
					{
						content:'我的提现失败了,是什么原因呢,请帮我查看一下, 联系方式:15788880000',date:'08:56',choose_index:0,x:0
					}
				],
				juli:0,
				c_index:null
			}
		},
		methods: {
			shanchu(index){
				this.list[index].choose_index=1;
			},
			moveMview(e,index){
				var that = this;
				if(this.list[index].x == 0){
					this.list[index].x = 1;
					that.juli = -100;
				}else{
					this.list[index].x = 0;
					that.juli = 0;
				}
				this.c_index = index;

			},
			onchange(e){
				console.log('e',e);
			}
		}
	}
</script>

### 实现 UniApp 中左滑动删除效果 在 UniApp 开发环境中实现滑动删除功能,主要依赖于 JavaScript 的 `touchstart`、`touchmove` 和 `touchend` 事件来捕捉用户的触屏行为并作出响应[^1]。 #### HTML 结构设计 首先定义好页面的基础结构,创建可被滑动的列表项: ```html <template> <view class="container"> <!-- 购物车商品列表 --> <block v-for="(item, index) in goodsList" :key="index"> <view class="goods-item" @touchstart="handleTouchStart(index)" @touchmove="handleTouchMove(index)" @touchend="handleTouchEnd(index)"> <view class="content">{{ item.name }}</view> <view class="delete-btn">删除</view> </view> </block> </view> </template> ``` #### CSS 样式设置 接着配置相应的样式使界面美观且易于操作: ```css <style scoped> .goods-item { display: flex; overflow: hidden; } .content { flex-grow: 1; background-color: white; } .delete-btn { width: 80px; color: red; text-align: center; } </style> ``` #### Vue 方法编写 最后,在脚本部分加入逻辑控制函数用于处理触摸事件以及执行实际的删除动作: ```javascript <script> export default { data() { return { startX: null, moveX: null, isSwiping: false, swipeThreshold: 50, // 定义触发侧滑的距离阈值 goodsList: [ { name: '苹果' }, { name: '香蕉' } ] }; }, methods: { handleTouchStart(index) { this.startX = event.touches[0].pageX; // 记录起始位置 this.isSwiping = true; }, handleTouchMove(index) { if (!this.isSwiping || !event.touches.length) return; const currentX = event.touches[0].pageX; this.moveX = this.startX - currentX; let el = document.querySelectorAll('.goods-item')[index]; if (Math.abs(this.moveX) > this.swipeThreshold && this.moveX > 0){ el.style.transform = `translateX(-${el.offsetWidth}px)` ; }else{ el.style.transform = ''; } }, handleTouchEnd(index) { this.isSwiping = false; setTimeout(() => { let el = document.querySelectorAll('.goods-item')[index]; if(Math.abs(this.moveX)>this.swipeThreshold&&this.moveX>0){ this.$set(this.goodsList,'splice',index,1); console.log('已删除'); } else { el.style.transition='transform .3s ease-out'; el.style.transform=''; } this.startX=null; this.moveX=0; }, 200); } } }; </script> ``` 上述代码实现了当用户向右滑动超过一定距离时显示“删除”按钮,并允许点击确认移除指定的商品条目;而如果未达到设定条件,则自动恢复原状。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值