新建文件longpress.ts
文件
// longpress.ts
import {
DirectiveBinding } from 'vue'
const longpress = {
beforeMount(el: any, binding: DirectiveBinding) {
const cb = binding.value
const duration = binding.arg || 800 // 长按等待时间
let timer: number | null = null
let isLongPress = false // 记录是否触发长按
const longPress = (e: TouchEvent) => {
if (e.type === 'touchstart') {
timer = window.setTimeout(() => {
isLongPress = true
if (typeof cb === 'function') cb(el) // 3. 回调函数
}, +duration)
return false
} else if (e.type === 'touchmove') {
if