uniapp列表左滑删除

文章描述了一个使用Vue.js编写的组件,演示了如何通过触摸事件实现商品列表中的滑动选择和删除功能,包括触摸开始、移动和结束时的处理逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
	<view class="container">
		<view class="item-all" @click.stop="change(item,index)" v-for="(item,index) in list" :key="index"
			@touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd(index)">
			<view class="item">
				<view class="item-left">
					<image class="img" src="../static/logo.png"></image>
				</view>
				<view class="item-ri">
					<view class="item-text4">
						<text>品名:</text>
						<text>商品编码:</text>
					</view>
					<view>
						<text class="item-text2">起批量:</text>
						<text class="item-text2">交期:</text>
						<text class="item-text3">¥43</text>
					</view>
				</view>
			</view>
			<view class="del" :class="{ hide: !item.select }">
				<image @click.stop="handleDel(index)" src="../static/images/del.png"></image>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "w-list",
		props: {
			isDel: {
				type: Boolean,
				default: false,
			}
		},
		data() {
			return {
				list: [{
						select: false
					},
					{
						select: false
					},
					{
						select: false
					},
					{
						select: false
					},
				],
				sleb: 0,
				timer: null,
				index: 0,

			};
		},
		methods: {
			touchStart(e) {
				// console.log(e);
				console.log('获取焦点', e.touches[0].clientX);
				this.sleb = e.touches[0].clientX;
			},
			touchMove(e) {
				this.timer = setTimeout(() => {
					// 排它
					this.list.forEach(item => {
						item.select = false;
					})
					const delTax = e.touches[0].clientX - this.sleb;
					if (delTax < -10) {
						// 向左滑动
						console.log("向左滑动", delTax);
						this.list[this.index].select = true;
					} else if (delTax > 10) {
						// 向右滑动
						console.log("向右滑动", delTax);
						this.list[this.index].select = false;
					}
				}, 300);
			},
			// 获取索引index
			touchEnd(i) {
				console.log('获取索引', i);
				this.index = i;
			},
			// 删除
			handleDel(i) {
				console.log("删除", i);
				this.list[i].select = false
			}
		}
	}
</script>

<style scoped>
	.hide {
		display: none;
	}

	.container {
		padding: 20rpx;
		box-sizing: border-box;
		padding-bottom: 110rpx;
		background-color: #fff;
	}

	.item-all {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		margin-bottom: 20rpx;
	}

	.select {
		width: 40rpx;
		height: 40rpx;
		margin-right: 20rpx;
	}

	.select-img {
		width: 40rpx;
		height: 40rpx;
	}

	.item {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		box-shadow: 0px 0px 5px #b8b6b7;
		padding: 20rpx;
		box-sizing: border-box;
		border-radius: 10rpx;
		width: 100% !important;
		height: 100% !important;

	}

	.item-left {}

	.img {
		width: 160rpx;
		height: 160rpx;
		border-radius: 10rpx;
	}

	.item-ri {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: center;
		width: 100%;
		margin-left: 20rpx;
	}

	.item-text1 {
		font-size: 30rpx;
		font-weight: bold;
	}

	.item-text2 {
		font-size: 27rpx;
	}

	.item-view {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		width: 100%;
	}

	.item-text3 {
		color: #ff0000;
	}

	.item-text4 {
		border-bottom: 1px solid #EDEDED;
		width: 100%;
		height: 100%;
		font-size: 30rpx;
	}

	.del {
		width: 80rpx;
		margin-left: 10rpx;
	}

	.del image {
		width: 60rpx;
		height: 60rpx;
	}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值