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

小程序按住说话

小程序按住说话、上滑取消的实现。
使用view作为容器总有奇怪的问题,比如touchend事件有时候不触发。
需要使用事件修饰符来阻止冒泡,不如有时候touchend、touchmove也不会触发。
下面是效果及源码。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<template><view class="w-full bg-white"><buttonclass="record-btn w-full"style="padding: 0; line-height: 22px; background-color: transparent"@touchend.stop="handleTouchEnd"@touchmove.stop="handleTouchmove"@longpress.stop="handleLongPress"><view v-if="isRecording"><view class="text-center text-secondary text-xs py-3"><text v-if="isUp">松开取消</text><text v-else>松手发送,上滑取消</text></view><Wave :bgColor="`var(--wot-color-${isUp ? 'danger' : 'theme'})`" /></view><view v-else class="leading-22px py-3"><view><text class="font-bold">按住说话</text></view></view></button></view>
</template><script lang="ts" setup>
import Wave from './Wave.vue'const isRecording = ref(false) // 状态,当前是否在录音
const isUp = ref(false) // 是否上滑
const startY = ref(0)
const handleLongPress = (event) => {startY.value = event.touches[0].clientYisRecording.value = trueconsole.log(`output->按压的坐标`, startY.value)
}
const handleTouchEnd = () => {isRecording.value = falsestartY.value = 0isUp.value = falseconsole.log(`output->离开后的坐标`, startY.value)
}
const handleTouchmove = (event) => {if (!isRecording.value) returnconst currentY = event.touches[0].clientYconst diffY = startY.value - currentY// 设定阈值,防止误判isUp.value = diffY > 10
}
</script>

wave.vue

<template><viewclass="wave-container":style="{'background-color': bgColor,}"><view :class="`wave-bar wave-bar-delay-${n}`" v-for="n in generateSequence(20)" :key="n"></view></view>
</template>
<script lang="ts" setup>
type PropsType = { bgColor: string }const props = defineProps<PropsType>()function generateSequence(n) {const result = []// 递增部分:1 到 nfor (let i = 1; i <= n; i++) {result.push(i)}// 递减部分:n-1 到 1for (let i = n - 1; i >= 1; i--) {result.push(i)}return result
}
</script><style lang="scss" scoped>
$number: 20;.wave-container {display: flex;gap: 5px;align-items: center;justify-content: center;width: 100%;height: 22px;padding: 12px 0;border-radius: 10px;
}
.wave-bar {width: 3px;height: 10px;background-color: #fff;border-radius: 2px;animation: wave 1s infinite ease-in-out;
}@for $i from 1 through $number {.wave-bar-delay-#{$i} {animation-delay: $i * -0.8 + s;}
}@keyframes wave {0%,100% {transform: scaleY(0.8);}50% {transform: scaleY(1);}
}
</style>
http://www.lryc.cn/news/591368.html

相关文章:

  • 紫金桥跨平台监控组态软件 | 功能强大,支持复杂工业场景,与西门子 PLC 无缝兼容
  • 【Linux基础知识系列】第五十二篇 - 初识Linux的内置命令
  • 三十四、【扩展工具篇】JSON 格式化与解析:集成 Monaco Editor 打造在线 JSON 工具
  • 物联网主机在化工园区安全风险智能化管控平台中的应用
  • day055-Dockerfile与常用指令
  • PyCharm 高效入门指南(引言 + 核心模块详解)
  • 【C# in .NET】16. 探秘类成员-索引器:通过索引访问对象
  • 关于接口测试的HTTP基础【接口测试】
  • 解读一个大学专业——信号与图像处理
  • 一种融合人工智能与图像处理的发票OCR技术,将人力从繁琐的票据处理中解放
  • 小红书获取关键词列表API接口详解
  • 在 Windows 上使用 Docker 运行 Elastic Open Crawler
  • Java爬虫与正则表达式——用正则来爬取数据
  • 利用deepspeed在Trainer下面微调大模型Qwen2.5-3B
  • 切比雪夫不等式的理解以及推导【超详细笔记】
  • 【Linux手册】缓冲区:深入浅出,从核心概念到实现逻辑
  • 2025年6月GESP(C++一级):假期阅读
  • 多线程--sem_wait(sem)特殊用法
  • 【原创】【图像算法】高精密电子仪器组装异常检测
  • 24、鸿蒙Harmony Next开发:不依赖UI组件的全局自定义弹出框 (openCustomDialog)
  • java之json转excel生成
  • AppTrace:重新定义免填邀请码,解锁用户裂变新高度
  • IMU噪声模型
  • JxBrowser 7.43.5 版本发布啦!
  • ubuntu 开启ssh踩坑之旅
  • 加速度传感器方向校准方法
  • 原生前端JavaScript/CSS与现代框架(Vue、React)的联系、区别与运行环境(精简版)
  • 关于用git上传远程库的一些常见命令使用和常见问题:
  • Python爬虫入门到实战(2)-selenium驱动浏览器
  • 静态住宅IP和节点有什么区别?哪种更适合你的需求?