uniapp列表左滑删除

<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>
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp 中实现购物车左滑删除商品的功能,你可以使用一些组件或库来实现。以下是一种常见的实现方式: 1. 首先,你需要使用 `<swiper>` 组件作为购物车列表的容器,并设置 `direction` 属性为 `horizontal`,使其能够横向滑动。 2. 在每个购物车商品项中,使用 `<swiper-item>` 组件包裹内容,并设置左滑删除的按钮。 3. 在左滑删除按钮上绑定一个事件,当用户点击按钮时,触发相应的事件处理函数。 4. 在事件处理函数中,根据商品的唯一标识(如商品ID)从购物车数组中找到对应的商品项,并将其从数组中移除。 5. 更新购物车数组后,可以选择将新的购物车数据存储在本地或发送到服务端进行更新。 下面是一个简单的示例代码,帮助你理解如何实现购物车左滑删除商品的功能: ```vue <template> <div> <swiper class="cart-swiper" direction="horizontal"> <swiper-item v-for="item in cartItems" :key="item.id"> <div class="cart-item"> <div class="item-content"> {{ item.name }} - {{ item.price }} </div> <div class="delete-btn" @click="removeFromCart(item.id)"> 删除 </div> </div> </swiper-item> </swiper> </div> </template> <script> export default { data() { return { cartItems: [ { id: 1, name: '商品1', price: 10 }, { id: 2, name: '商品2', price: 20 }, { id: 3, name: '商品3', price: 30 } ] } }, methods: { removeFromCart(itemId) { this.cartItems = this.cartItems.filter(item => item.id !== itemId); } } } </script> <style scoped> .cart-swiper { width: 100%; height: 100%; } .cart-item { display: flex; align-items: center; height: 100px; } .item-content { flex: 1; } .delete-btn { width: 100px; background-color: red; color: white; text-align: center; } </style> ``` 以上示例代码使用了 `<swiper>` 和 `<swiper-item>` 组件来实现左滑删除功能,需要注意的是,你可能还需要根据实际情况对样式进行调整。希望能帮到你!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值