当前位置: 首页 > news >正文

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

uni-app移动端实现触摸滑动功能:展开收起、滑动删除;根据X轴或者Y轴的坐标,计算差值,当差值超出设置的滑动阈值(自定义大小),执行所需要的功能。
一、滑动展开收起内容
实现:弹出框默认展示部分内容,手触摸当前内容,向上滑动显示全部内容,向下滑动收起页面展示默认的内容;

<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>
<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>
<style lang="scss" scoped>.popup-container {.device-other {transition: max-height 0.3s ease;max-height: 0rpx; /* 初始高度 */&.show-all {max-height: 700rpx; /* 展开时高度 */}}}
</style>

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

<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>
<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>
http://www.dtcms.com/a/581890.html

相关文章:

  • 各 DXE Driver 作用
  • 开源视觉-语言-动作(VLA)机器人项目全景图(截至 2025 年)
  • 元宇宙:数字文明的下一站(元宇宙概念的理解及畅想(什么是元宇宙?未来它将如何改变世界?)
  • 20251106给荣品RD-RK3588-MID开发板跑Rockchip的原厂Android13系统时适配AP6275P模块的BT【使用荣品的DTS】
  • DS ArrayLinkedList、StackQueue、TreeMap、SearchSort
  • 网站建设与管理培训方案做网站可以卖钱吗
  • 太原制作网站的公司网站网站 换图片
  • 渗透测试数据库判断卡壳?分类 + 方法 + SQL/NoSQL 脚本速用
  • 低价网站建设策划内容怎么做公司网站的二维码
  • UniHttp/Jsoup Https SSL证书验证失败:SunCertPathBuilderException解决方案详解
  • Notepad++ 7.8.4 安装步骤详解(附 npp.7.8.4.Installer 安装教程)
  • 柳市网站建设哪家好wordpress七牛图片插件
  • 用 MCP 重构 RAG 智能体:3 步解决数据安全与多工具协同难题
  • QOS服务质量
  • 如何为视频进行外语配音:分步指南
  • 建设部网站投诉核查企业名单网页和网站的概念
  • kanass零基础学习,项目负责人如何使用kanass驾驭项目
  • redis实战篇day04
  • 罗湖网站公司服务器网站管理助手
  • 八股训练营第 10 天 | 进程和线程之间有什么区别?并行和并发有什么区别?解释一下用户态和核心态,什么场景下,会发生内核态和用户态的切换?
  • AIDAv2:重新定义DeFi的AI驱动金融基础设施
  • SAP PP未清生产订单关闭物料退料、新工单发料批量处理
  • 下载软件的网站哪个好用哪个网站做首页好
  • 【零基础一站式指南】Conda 学习环境准备与 Jupyter/PyCharm 完全配置
  • 滨州制作网站深圳优化公司
  • mysql第四次做业
  • clusterProfile包用于宏基因组学富集分析
  • 湖北网站开发培训写一篇软文多少钱
  • python+django/flask基于协同过滤算法的理财产品推荐系统
  • h5个网站的区别某颜值女主播低俗内容流出视频