当前位置:首页 > VUE

vue 实现长按删除

2026-02-18 15:09:07VUE

实现长按删除功能

在Vue中实现长按删除功能,可以通过监听touchstarttouchend事件,结合定时器来实现。以下是一个完整的实现方案:

监听触摸事件

在需要长按删除的元素上绑定touchstarttouchend事件。touchstart事件用于启动计时器,touchend事件用于取消计时器。

<template>
  <div 
    @touchstart="startPress"
    @touchend="endPress"
  >
    长按我删除
  </div>
</template>

设置计时器

startPress方法中设置一个计时器,延迟执行删除操作。通常长按的时间阈值设置为500ms到1s。

<script>
export default {
  data() {
    return {
      pressTimer: null
    }
  },
  methods: {
    startPress() {
      this.pressTimer = setTimeout(() => {
        this.deleteItem()
      }, 1000) // 1秒后触发删除
    },
    endPress() {
      clearTimeout(this.pressTimer)
    },
    deleteItem() {
      // 执行删除逻辑
      console.log('删除操作')
    }
  }
}
</script>

添加视觉反馈

为了提高用户体验,可以在长按时添加视觉反馈,比如改变背景色或显示提示。

<template>
  <div 
    @touchstart="startPress"
    @touchend="endPress"
    :class="{ 'active': isPressing }"
  >
    长按我删除
  </div>
</template>
<script>
export default {
  data() {
    return {
      pressTimer: null,
      isPressing: false
    }
  },
  methods: {
    startPress() {
      this.isPressing = true
      this.pressTimer = setTimeout(() => {
        this.deleteItem()
      }, 1000)
    },
    endPress() {
      this.isPressing = false
      clearTimeout(this.pressTimer)
    }
  }
}
</script>

兼容鼠标事件

如果需要同时支持鼠标操作,可以添加mousedownmouseup事件。

<template>
  <div 
    @touchstart="startPress"
    @touchend="endPress"
    @mousedown="startPress"
    @mouseup="endPress"
    @mouseleave="endPress"
  >
    长按我删除
  </div>
</template>

防止误触

为了避免误触,可以在touchend时检查手指移动距离,如果移动过大则取消删除操作。

methods: {
  startPress(e) {
    this.startX = e.touches ? e.touches[0].clientX : e.clientX
    this.startY = e.touches ? e.touches[0].clientY : e.clientY
    this.isPressing = true
    this.pressTimer = setTimeout(() => {
      this.deleteItem()
    }, 1000)
  },
  endPress(e) {
    const endX = e.changedTouches ? e.changedTouches[0].clientX : e.clientX
    const endY = e.changedTouches ? e.changedTouches[0].clientY : e.clientY
    const distance = Math.sqrt(Math.pow(endX - this.startX, 2) + Math.pow(endY - this.startY, 2))
    if (distance > 10) {
      clearTimeout(this.pressTimer)
    }
    this.isPressing = false
  }
}

完整示例

以下是一个完整的Vue组件示例:

<template>
  <div 
    @touchstart="startPress"
    @touchend="endPress"
    @mousedown="startPress"
    @mouseup="endPress"
    @mouseleave="endPress"
    :class="{ 'active': isPressing }"
    class="long-press-item"
  >
    长按我删除
  </div>
</template>

<script>
export default {
  data() {
    return {
      pressTimer: null,
      isPressing: false,
      startX: 0,
      startY: 0
    }
  },
  methods: {
    startPress(e) {
      this.startX = e.touches ? e.touches[0].clientX : e.clientX
      this.startY = e.touches ? e.touches[0].clientY : e.clientY
      this.isPressing = true
      this.pressTimer = setTimeout(() => {
        this.deleteItem()
      }, 1000)
    },
    endPress(e) {
      const endX = e.changedTouches ? e.changedTouches[0].clientX : e.clientX
      const endY = e.changedTouches ? e.changedTouches[0].clientY : e.clientY
      const distance = Math.sqrt(Math.pow(endX - this.startX, 2) + Math.pow(endY - this.startY, 2))
      if (distance > 10) {
        clearTimeout(this.pressTimer)
      }
      this.isPressing = false
    },
    deleteItem() {
      alert('删除成功')
    }
  }
}
</script>

<style>
.long-press-item {
  padding: 20px;
  background: #f0f0f0;
  margin: 20px;
}
.long-press-item.active {
  background: #ffeb3b;
}
</style>

通过以上方法,可以在Vue中实现一个完整的长按删除功能,包括触摸和鼠标支持、视觉反馈以及防误触处理。

vue 实现长按删除

标签: vue
分享给朋友:

相关文章

vue实现导航栏切图

vue实现导航栏切图

Vue 实现导航栏切图 使用 Vue Router 实现基础导航 在 Vue 项目中,可以通过 Vue Router 实现导航栏的路由切换。创建一个导航栏组件,并在其中使用 <router-li…

vue实现socket

vue实现socket

Vue 中实现 WebSocket 通信 在 Vue 项目中实现 WebSocket 通信可以通过原生 WebSocket API 或第三方库(如 socket.io-client)完成。以下是两种常…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

vue如何实现

vue如何实现

Vue 实现方法 在 Vue 中实现功能通常涉及组件化开发、状态管理和响应式数据绑定。以下是几种常见的实现方式: 组件化开发 使用 Vue 的单文件组件(.vue 文件)结构,将 UI 拆分为可复…

vue实现路径

vue实现路径

Vue 实现路径的方法 在 Vue 中实现路径管理通常涉及 Vue Router 的使用,以下是一些常见的实现方法: 安装 Vue Router 通过 npm 或 yarn 安装 Vue Route…

vue实现通讯

vue实现通讯

Vue 组件通讯方法 父子组件通讯 父组件向子组件传递数据通过 props,子组件向父组件传递数据通过 $emit 事件。 父组件模板: <child-component :messag…