当前位置: 首页 > 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.dtcms.com/a/284830.html

相关文章:

  • zlmediakit接入Onvif设备方案
  • The Missing Semester of Your CS Education 学习笔记以及一些拓展知识(二)
  • 嵌入式基础 -- ADC(模数转换器,Analog to Digital Converter)
  • 疯狂星期四文案网第10天运营日报
  • Kotlin自定义排序
  • AUTOSAR进阶图解==>AUTOSAR_SWS_EFXLibrary
  • Lotus-基于大模型的查询引擎 -开源学习整理
  • 打印文件/打印机队列 - 华为OD机试真题(Java 题解)
  • QT 交叉编译环境下,嵌入式设备显示字体大小和QT Creator 桌面显示不一致问题解决
  • 中国力学大会倒计时2天●千眼狼科学仪器在实验力学研究中应用
  • 状态机(State Machine)是什么?
  • 【秋招ready】
  • 网络初级安全第二次作业
  • css样式中的选择器和盒子模型
  • JoditEditor编辑与预览模式
  • 电碳表:精准计量每一度电的碳排放
  • Python--plist文件的读取
  • 使用CosyVoice-300M实现零样本语音克隆:Xinference部署与实战
  • nginx代理websocket请求
  • Android设备标识符详解:IMEI、ANDROID_ID与OAID
  • 产品经理如何绘制服务蓝图(Service Blueprint)
  • 企业级AI智能体架构落地:工程化能力设计的全景指南
  • docker重新搭建redis集群
  • ubuntu系统+N卡 | docker compose+ollama+dify
  • ACOUSLIC-AI挑战报告:基于低收入国家盲扫超声数据的胎儿腹围测量|文献速递-医学影像算法文献分享
  • 【LeetCode刷题指南】--数组串联,合并两个有序数组,删除有序数组中的重复项
  • FreeBSD Conda Python3.12下安装GPT4Free(g4f)0.5.7.3版本
  • VR全景园区:开启智慧园区新时代
  • 2025年5大国产ETL工具横向评测
  • 【面板数据】上市公司股价同步性数据集-dta+xlsx(2000-2023年)