当前位置:首页 > VUE

利用vue实现长按事件

2026-02-24 14:27:41VUE

监听原生事件实现长按

在Vue中可以通过监听touchstarttouchendmousedownmouseup原生事件来实现长按功能。创建一个计时器,当按下时间超过设定阈值时触发长按操作。

<template>
  <button 
    @mousedown="startPress"
    @mouseup="endPress"
    @touchstart="startPress"
    @touchend="endPress"
  >
    长按我
  </button>
</template>

<script>
export default {
  data() {
    return {
      pressTimer: null,
      pressDuration: 1000 // 1秒长按阈值
    }
  },
  methods: {
    startPress(e) {
      this.pressTimer = setTimeout(() => {
        this.handleLongPress(e)
      }, this.pressDuration)
    },
    endPress() {
      clearTimeout(this.pressTimer)
    },
    handleLongPress(e) {
      console.log('长按事件触发', e)
    }
  }
}
</script>

使用自定义指令封装

将长按逻辑封装为可复用的自定义指令,使用时只需在元素上添加v-longpress指令。

利用vue实现长按事件

// 全局注册指令
Vue.directive('longpress', {
  bind(el, binding) {
    let pressTimer = null
    const handler = binding.value
    const duration = binding.arg || 1000

    const start = (e) => {
      if (pressTimer === null) {
        pressTimer = setTimeout(() => {
          handler(e)
        }, duration)
      }
    }

    const cancel = () => {
      if (pressTimer !== null) {
        clearTimeout(pressTimer)
        pressTimer = null
      }
    }

    el.addEventListener('mousedown', start)
    el.addEventListener('touchstart', start)
    el.addEventListener('mouseup', cancel)
    el.addEventListener('touchend', cancel)
    el.addEventListener('mouseleave', cancel)
  }
})

// 使用方式
<button v-longpress:1500="longPressHandler">长按1.5秒</button>

处理移动端与PC端兼容

针对不同设备需要处理事件差异,防止同时触发点击和长按事件。可以添加标志位判断是否已触发长按。

利用vue实现长按事件

methods: {
  startPress(e) {
    this.isLongPress = false
    this.pressTimer = setTimeout(() => {
      this.isLongPress = true
      this.handleLongPress(e)
    }, this.pressDuration)
  },
  endPress() {
    clearTimeout(this.pressTimer)
    if (!this.isLongPress) {
      this.handleClick()
    }
  },
  handleClick() {
    console.log('普通点击事件')
  }
}

添加触觉反馈(移动端)

在移动设备上触发长按时,可以通过振动API提供触觉反馈,增强用户体验。

handleLongPress(e) {
  if ('vibrate' in navigator) {
    navigator.vibrate(50) // 振动50毫秒
  }
  console.log('长按事件触发')
}

防止长按默认行为

某些浏览器在长按时会触发默认菜单,需要阻止这些默认行为以获得一致体验。

startPress(e) {
  e.preventDefault()
  // 剩余长按逻辑
}

以上方法可根据实际需求组合使用,自定义指令方式更适合在多个组件中复用长按逻辑,而原生事件监听则适合简单场景。移动端开发时务必同时处理touchmouse事件以确保兼容性。

标签: 事件vue
分享给朋友:

相关文章

vue实现截图

vue实现截图

Vue 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2…

vue实现input

vue实现input

Vue 实现 Input 输入框的方法 基本双向绑定 使用 v-model 指令实现双向数据绑定,适用于大多数表单场景: <template> <input v-model=…

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

vue实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖…

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的…

vue 实现modal

vue 实现modal

Vue 实现 Modal 的方法 使用 Vue 原生组件 通过 Vue 的组件系统可以轻松实现 Modal。创建一个可复用的 Modal 组件,通过 props 控制显示与隐藏。 <temp…