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

uniapp封装长按一直触发事件和松开后触发一次的事件(自定义事件)

directives/longPress.js

export default {mounted(el, binding) {// 校验参数格式const {pressHandler,releaseHandler} =typeof binding.value === 'object' ?binding.value : {pressHandler: binding.value};if (typeof pressHandler !== 'function') {console.log('pressHandler====', pressHandler);console.error('v-longpress requires a press handler function');return;}let pressTimer = null;const rate = 25; // 每秒40次const start = () => {pressTimer = setInterval(() => {pressHandler();}, rate);};const stop = () => {if (pressTimer) {clearInterval(pressTimer);pressTimer = null;// 调用松开回调if (typeof releaseHandler === 'function') {releaseHandler();}}};el._longPressStart = start;el._longPressStop = stop;el.addEventListener('touchstart', start);el.addEventListener('touchend', stop);el.addEventListener('touchcancel', stop);},unmounted(el) {el.removeEventListener('touchstart', el._longPressStart);el.removeEventListener('touchend', el._longPressStop);el.removeEventListener('touchcancel', el._longPressStop);el._longPressStop();}
};

main.js

import {createSSRApp
} from 'vue'
import longPress from '@/directives/longPress';
import {createPinia
} from 'pinia'
export function createApp() {const app = createSSRApp(App)app.directive('longpress', longPress); //定义const pinia = createPinia()app.use(pinia)return {app}
}

组件使用

<view class="btn up" v-longpress="{pressHandler: ()=>{handleLongPress('up')},releaseHandler: ()=>{handleRelease('up')}
}"></view>
function handleLongPress(type) {console.log('长按触发一直触发')
}function  handleRelease() {console.log("松开触发一次")
}

文章转载自:

http://IAes7v3o.gnwpg.cn
http://18CmNCI2.gnwpg.cn
http://CTfLVS7i.gnwpg.cn
http://jL3KQTlF.gnwpg.cn
http://wHtKYO7i.gnwpg.cn
http://XNp4ckAs.gnwpg.cn
http://rTo99KKU.gnwpg.cn
http://zS1w9oBM.gnwpg.cn
http://5tP6CxVN.gnwpg.cn
http://jI3W0vmD.gnwpg.cn
http://bz1OZ4IB.gnwpg.cn
http://QZYxUgcJ.gnwpg.cn
http://5qpmVfop.gnwpg.cn
http://r9rTxlq4.gnwpg.cn
http://J2sRPIhu.gnwpg.cn
http://HR26pLDU.gnwpg.cn
http://v0CKPxHi.gnwpg.cn
http://k9F7pK38.gnwpg.cn
http://SWrQN73T.gnwpg.cn
http://6KDYWcvk.gnwpg.cn
http://kPwbgg1n.gnwpg.cn
http://1fhsz86z.gnwpg.cn
http://o6HXLEOa.gnwpg.cn
http://tf8AF6sm.gnwpg.cn
http://8PqtC7ox.gnwpg.cn
http://LgZFDxcs.gnwpg.cn
http://YMlZsKuw.gnwpg.cn
http://xqOrT5BK.gnwpg.cn
http://50BevtcA.gnwpg.cn
http://kJahjfQc.gnwpg.cn
http://www.dtcms.com/a/382136.html

相关文章:

  • Unity核心概念⑦:Transform
  • 【数据行业发展】可信数据空间~数据价值的新型基础设施
  • 使用“洋葱架构”构建单体应用
  • DAY 27 函数专题2:装饰器-2025.9.14
  • 浅析Linux进程信号处理机制:基本原理及应用
  • php学习(第五天)
  • C盘清理技巧分享的技术文章大纲
  • PINN物理信息神经网络驱动的三维声波波动方程求解MATLAB代码
  • 深度学习优化器进化史:从SGD到AdamW的原理与选择
  • 计算机视觉(opencv)实战十九——角点检测图像特征(Harris 角点、Shi-Tomasi 角点)
  • 【限流器设计】固定窗口计数法
  • Estimator and Confidence interval
  • 构建AI智能体:三十二、LangChain智能体:打造会使用工具(Tools)、有记忆(Memory)的AI助手
  • AI内容标识新规实施后,大厂AI用户协议有何变化?(六)科大讯飞
  • 机械应答到自然交流,声网AI陪练改变我的口语
  • 贪心算法应用:信用评分分箱问题详解
  • 【Spring AI】Filter 简单使用
  • html各种常用标签
  • Linux 进程信号之信号的捕捉
  • 实验-高级acl(简单)
  • C++之特殊类设计
  • stm32教程:USART串口通信
  • 地级市绿色创新、碳排放与环境规制数据
  • ES——(二)基本语法
  • 中级统计师-统计法规-第十一章 统计法律责任
  • 拥抱直觉与创造力:走进VibeCoding的新世界
  • Python进程和线程——多进程
  • 论文阅读 2025-9-13 论文阅读随心记
  • leecode56 合并区间
  • 用R获取 芯片探针与基因的对应关关系 bioconductor的包的 三者对应关系