uniapp实现左滑删除的效果



uniapp 左滑删除效果一、效果二(2个方式自选其一)(整理)_uniapp单元左滑操作-优快云博客

参考的上面的博客

下面是自己的实现的效果:

代码:

<template>
	<view class="">
		<scroll-view :scroll-y="isScroll" :style="{ height: windowHeight + 'px' }">
			<block :key="item.id" v-for="(item, index) in dataList">
				<view :data-index="index" class="shop-cart-list-item" @touchstart="drawStart" @touchmove="drawMove"
					@touchend="drawEnd" :style="{ right: item.right + 'rpx'}">
					<image src="../../static/qhj.png" mode="widthFix" class="img"></image>
					<view class="desc">
						<view class="title">啦啦啦啦啦啦啊啊啊啊啊啊啊啊啊啊啊啊啊啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦</view>
						<view class="num">
							<view class="price"><text>¥</text> 999</view>
							<view class="shu">
								<text class="jian" :class="item.number==1?'disable':''" @click="onJian(item)">-</text>
								<text class="num">{{item.number}}</text>
								<text class="jia" @click="onJia(item)">+</text>
							</view>
						</view>
					</view>
					<view class="content">{{ item.content }}</view>
					<view @click="delItem(index, $event)">
					<view class="remove" >删除</view>
					</view>
				</view>
			</block>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
				delBtnWidth: 160,
				dataList: [{
					id: 1,
					number: '1',
					content: '1',
					right: 0
				}, {
					id: 2,
					number: '1',
					content: '2',
					right: 0
				}, {
					id: 3,
					number: '1',
					content: '3',
					right: 0
				}, {
					id: 4,
					number: '1',
					content: '4',
					right: 0
				}, {
					id: 5,
					number: '1',
					content: '5',
					right: 0
				}, {
					id: 6,
					number: '1',
					content: '6',
					right: 0
				}, {
					id: 7,
					number: '1',
					content: '7',
					right: 0
				}, {
					id: 8,
					number: '1',
					content: '8',
					right: 0
				}, ],
				isScroll: true,
				windowHeight: 0
			};
		},
		onLoad: function(options) {
			var that = this;
			wx.getSystemInfo({
				success: function(res) {
					that.windowHeight = res.windowHeight;
				}
			});
		},
		methods: {
			drawStart: function(e) {
				// console.log("drawStart");
				var touch = e.touches[0];
				console.log(touch, 'touch');
				for (var index in this.dataList) {
					this.dataList[index].right = 0;
				}
				this.startX = touch.clientX;
			},
			drawMove: function(e) {
				var touch = e.touches[0];
				var item = this.dataList[e.currentTarget.dataset.index];
				var disX = this.startX - touch.clientX;

				if (disX >= 20) {
					if (disX > this.delBtnWidth) {
						disX = this.delBtnWidth;
					}
					this.isScroll = false;
					this.dataList[e.currentTarget.dataset.index].right = disX;
				} else {
					this.isScroll = true;
					this.dataList[e.currentTarget.dataset.index].right = 0;
				}
			},
			drawEnd: function(e) {
				var item = this.dataList[e.currentTarget.dataset.index];
				if (item.right >= this.delBtnWidth / 2) {
					this.isScroll = true;
					this.dataList[e.currentTarget.dataset.index].right = this.delBtnWidth;
				} else {
					this.isScroll = true;
					this.dataList[e.currentTarget.dataset.index].right = 0;
				}
			},
			delItem(index, event) {
				 console.log("Deleting item at index:", index);
			    event.stopPropagation(); // 阻止事件冒泡
			    event.preventDefault(); // 阻止默认行为
			    this.dataList.splice(index, 1);
			},
			//商品数量变化——加
			onJia(item) {
			    item.number = parseInt(item.number) + 1;
			},
			//商品数量变化——减
			onJian(item) {
			    if (parseInt(item.number) > 1) {
			        item.number = parseInt(item.number) - 1;
			    }
			}
		
		}
	}
</script>

<style scoped>
	.shop-cart-list-item {
		height: 240rpx;
		width: 100%;
		display: flex;
		position: relative;
		background-color: #FFFFFF;
		transition: all 0.2s;
		margin-bottom: 50rpx;

		.radio {
			line-height: 180rpx;
			margin-right: 20rpx;
		}

		.img {
			width: 180rpx;
			height: 180rpx;
			flex-shrink: 0;
		}

		.desc {
			width: 100%;
			padding: 0 20rpx;

			.title {
				color: #101010;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
			}

			.num {
				display: flex;
				justify-content: space-between;
				margin-top: 20rpx;

				.price {
					font-size: 36rpx;
					color: #FF1616;
					font-weight: bold;
				}

				.shu {
					color: #888888;

					.jian,
					.jia,
					.num {
						width: 40rpx;
						height: 40rpx;
						line-height: 40rpx;
						display: inline-block;
						text-align: center;
						color: #333333;
					}

					.jian,
					.jia {
						border: 1px solid #333333;
						border-radius: 100%;
					}

					.disable {
						border-color: #888888;
						color: #888888;
					}
				}
			}
		}
	}

	.remove {
		width: 180rpx;
		/* 增加宽度 */
		height: 100%;
		background-color: red;
		color: white;
		position: absolute;
		top: 0;
		right: -180rpx;
		/* 根据新的宽度调整 */
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 100;
		/* 确保是最高 */
	}
</style>

### UniApp实现聊天列表删除功能 在开发移动应用时,提供良好的用户体验至关重要。对于聊天应用程序而言,在消息列表中加入删除的功能可以显著提升用户的交互体验。 为了实现UniApp 中创建带有删除效果的消息列表,推荐采用 `uni-swipe-action` 组件来简化操作[^1]: #### 使用 uni-swipe-action 实现删除 此组件允许开发者轻松定义可被用户横向动触发的操作按钮集合。下面是一个简单的例子展示如何利用该组件构建具有删除特性的聊天记录项: ```html <template> <view class="message-list"> <!-- 循环渲染每一条消息 --> <block v-for="(msg, index) in messages" :key="index"> <uni-swipe-action> <uni-swipe-action-item @click="onDelete(index)"> <view class="message-item">{{ msg.text }}</view> </uni-swipe-action-item> <template slot="right"> <button type="warn">删除</button> </template> </uni-swipe-action> </block> </view> </template> <script> export default { data() { return { messages: [ { text: '你好啊' }, { text: '最近过得怎么样?' } ] }; }, methods: { onDelete(index) { this.messages.splice(index, 1); } } }; </script> <style scoped> .message-list .message-item { padding: 20rpx; } </style> ``` 上述代码片段展示了怎样通过 Vue.js 的模板语法以及 UniApp 提供的自定义组件快速搭建起支持手势并能执行相应动作(这里是移除项目)的小部件。当用户向动某个特定条目时,“删除”按钮将会显现出来;一旦按下它,则会调用预设的方法完成数据更新逻辑[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值