uniapp移动端实现触摸滑动功能:上下滑动展开收起内容,左右滑动删除列表

uni-app移动端实现触摸滑动功能:展开收起、滑动删除;根据X轴或者Y轴的坐标,计算差值,当差值超出设置的滑动阈值(自定义大小),执行所需要的功能。

一、滑动展开收起内容

实现:弹出框默认展示部分内容,手触摸当前内容,向上滑动显示全部内容,向下滑动收起页面展示默认的内容;

html 复制代码
<template>
	<u-popup class="custom-popup" :show="showPopup" mode="bottom" :mask-close-able="true" :closeable="true"
		:overlay="false" customStyle="border-radius: 30rpx 30rpx 0rpx 0rpx;" zIndex="1001" bgColor="transparent"
		@close="closePopup">
		<!-- 弹出内容 -->
		<view class="popup-container" @touchstart="handleTouchStart" @touchmove="handleTouchMove"
			@touchend="handleTouchEnd">
			<view class="device-top flex">
				<!-- 默认显示内容 -->
			</view>
			<view class="device-other" :class="{ 'show-all': isExpanded }">
				<!-- 展开内容 -->
			</view>
		</view>
	</u-popup>
</template>
javascript 复制代码
<script>
	export default {
		name: "UsbBlue",
		props: {
			showPopup: {
				type: Boolean,
				default: false
			},
			deviceData: {
				type: Object,
				default: null
			},
		},
		data() {
			return {
				isExpanded: false,
				isDragging: false,
				startY: 0,
			};
		},
		mounted() {},
		methods: {
			handleTouchStart(event) {
				this.isDragging = true;
				this.startY = event.touches[0].clientY; // 记录开始时的Y坐标
			},
			handleTouchMove(event) {
				if (!this.isDragging) return;
				const currentY = event.touches[0].clientY; // 当前触摸的Y坐标
				const diffY = this.startY - currentY; // 计算Y方向的移动距离(正值表示向下拖动,负值表示向上拖动)
				console.log(diffY);
				if (diffY > 10 && !this.isExpanded) { // 向上滑动时展开
					this.isExpanded = true;
					this.isDragging = false;
				} else if (diffY < -20 && this.isExpanded) { // 向下滑动时收起
					this.isExpanded = false;
					this.isDragging = false;
				} else if (diffY < -50) { // 大幅向下滑动时关闭弹窗
					this.closePopup();
					this.isDragging = false;
				}
			},
			handleTouchEnd() {},
			closePopup() {
				this.$emit("closePopup")
			}
		}
	}
</script>
css 复制代码
<style lang="scss" scoped>
	.popup-container {
		.device-other {
			transition: max-height 0.3s ease;
			max-height: 0rpx; /* 初始高度 */

			&.show-all {
				max-height: 700rpx; /* 展开时高度 */
			}
		}
	}
</style>

二、左右滑动清除列表

实现:手触摸列表,左右滑动清除当前列表;

html 复制代码
<template>
	<view class="device-tips">
		<view class="device-tips-item flex" v-for="(item,index) in deviceList" :key="index"
			@touchstart="(e) => handleTouchStart(e, index)" @touchmove="(e) => handleTouchMove(e, index)"
			@touchend="(e) => handleTouchEnd(e, index)">
			<!-- 列表内容 -->
		</view>
	</view>
</template>
javascript 复制代码
<script>
	export default {
		data() {
			return {
				deviceList: [{
					name: "设备名称",
					id: "H9K018008Y",
					count: 2,
				}, {
					name: "Unknow",
					id: "9012008Y",
					count: 5,
				}],
				startX: 0,
				currentIndex: -1,
			}
		},
		mounted() {},
		methods: {
			// 删除数据
			deleteData() {
				this.deviceList.splice(this.currentIndex, 1)
				// 删除后清理触摸状态
				this.currentIndex = null;
			},
			// 开始触摸
			handleTouchStart(event, index) {
				event.preventDefault();
				event.stopPropagation();
				this.startX = event.touches[0].clientX;
				this.currentIndex = index;
			},
			// 滑动中
			handleTouchMove(event, index) {
				const currentX = event.touches[0].clientX;
				const diffX = this.startX - currentX;
				// 左右滑动清除
				if (diffX > 200 || diffX < -200) {
					console.log(index, diffX, this.currentIndex);
					if (index === this.currentIndex) {
						this.deleteData(this.currentIndex);
						// 删除后清理触摸状态,防止重复触发
						this.startX = null;
					}
				}
			},

			handleTouchEnd(event, index) {},
		}
	}
</script>
相关推荐
2501_944448005 小时前
Flutter for OpenHarmony衣橱管家App实战:支持我们功能实现
android·javascript·flutter
人工智能训练11 小时前
【极速部署】Ubuntu24.04+CUDA13.0 玩转 VLLM 0.15.0:预编译 Wheel 包 GPU 版安装全攻略
运维·前端·人工智能·python·ai编程·cuda·vllm
会跑的葫芦怪11 小时前
若依Vue 项目多子路径配置
前端·javascript·vue.js
xiaoqi92212 小时前
React Native鸿蒙跨平台如何进行狗狗领养中心,实现基于唯一标识的事件透传方式是移动端列表开发的通用规范
javascript·react native·react.js·ecmascript·harmonyos
jin12332212 小时前
React Native鸿蒙跨平台剧本杀组队消息与快捷入口组件,包含消息列表展示、快捷入口管理、快捷操作触发和消息详情预览四大核心功能
javascript·react native·react.js·ecmascript·harmonyos
烬头882114 小时前
React Native鸿蒙跨平台实现二维码联系人APP(QRCodeContactApp)
javascript·react native·react.js·ecmascript·harmonyos
pas13614 小时前
40-mini-vue 实现三种联合类型
前端·javascript·vue.js
摇滚侠14 小时前
2 小时快速入门 ES6 基础视频教程
前端·ecmascript·es6
2601_9498333914 小时前
flutter_for_openharmony口腔护理app实战+预约管理实现
android·javascript·flutter
珑墨15 小时前
【Turbo】使用介绍
前端