Vue中文社区

Vue3 手势指令发布!

在 Vue3 开发中,框架本身并没有内置丰富的手势指令(如滑动、捏合、长按等),开发者只能使用基础的鼠标/触摸事件绑定(如 @click、@mousedown、@touchstart 等)。这在需要复杂手势交互的移动端应用开发中,往往显得力不从心。

好消息是,vue3-touch-events 来了!这是一个专为 Vue3 设计的强大手势事件库,让你轻松实现各种触摸手势交互,完美支持移动端和桌面端。

手势演示
手势演示

什么是 vue3-touch-events?

vue3-touch-events 是一个 Vue3 插件,它为任何 HTML DOM 元素提供了丰富的触摸事件支持。通过这个库,你可以:

  • ✅ 轻松实现点击、长按、滑动、拖拽、缩放等手势
  • ✅ 一套代码同时支持移动端和桌面端
  • ✅ 使用简洁的声明式语法
  • ✅ 无需编写复杂的触摸事件处理逻辑
拖拽演示
拖拽演示

vue3-touch-events 的核心优势

🚀 跨平台完美兼容

  • 移动端:完美支持各种触摸手势
  • 桌面端:自动将触摸事件映射为鼠标事件
  • 一套代码,多端运行,无需额外适配

🎯 丰富的手势支持

手势类型
支持的事件
适用场景
点击类tap
、longtap、hold
普通点击、长按菜单、按压反馈
滑动类swipe
、swipe.left、swipe.right、swipe.top、swipe.bottom
轮播图、删除操作、页面切换
拖拽类press
、drag、drag.once、release
拖拽排序、拖拽上传、滑块
缩放类zoom
、zoom.in、zoom.out
图片缩放、地图缩放、画布操作

⚡ 性能优化

  • 事件节流:防止事件触发过于频繁导致性能问题
  • 自定义频率:可调整事件触发频率,平衡流畅度和性能
  • 轻量级:无依赖,打包体积小

🎨 开发体验优秀

  • 声明式语法:直观易懂,学习成本低
  • TypeScript 支持:完整的类型定义
  • 自动样式管理:通过 v-touch-class 自动管理交互状态

🚀 快速开始

安装

npm install vue3-touch-events

基本使用

// main.js
import Vue3TouchEvents from'vue3-touch-events'

const app = createApp(App)
app.use(Vue3TouchEvents)
app.mount('#app')
<!-- 在组件中使用 -->
<template>
<div>
<!-- 点击事件 -->
<buttonv-touch:tap="handleTap">点击我</button>

<!-- 长按事件 -->
<divv-touch:longtap="handleLongTap">长按我 2 秒</div>

<!-- 左滑删除 -->
<divv-touch:swipe.left="handleSwipeLeft">左滑删除我</div>

<!-- 拖拽排序 -->
<divv-touch:drag="handleDrag">拖拽我</div>

<!-- 缩放图片 -->
<imgv-touch:zoom="handleZoom"src="image.jpg"alt="缩放图片">

<!-- 自动交互样式 -->
<buttonv-touch:tap="handleTap"v-touch-class="active">点击有高亮</button>
</div>
</template>

<script>
exportdefault {
methods: {
    handleTap() {
console.log('元素被点击了!')
    },
    handleLongTap() {
console.log('长按事件触发!')
    },
    handleSwipeLeft(direction) {
console.log('向左滑动,方向:', direction)
    },
    handleDrag(event) {
console.log('拖拽中...')
    },
    handleZoom(zoomFactor) {
console.log('缩放因子:', zoomFactor)
    }
  }
}
</script>

<style>
.active {
background-color: #e0e0e0;
transform: scale(0.95);
}
</style>

Vue 2 vs Vue 3 版本对比

特性
Vue 2 版本
Vue 3 版本
事件命名touchhold
、start、end、moved、moving
hold
、press、release、drag.once、drag
性能
良好
更优(利用 Vue 3 的优化)
TypeScript
部分支持
完整支持
组合式 API
不支持
完全支持
事件节流
基础支持
增强支持,可自定义频率

从 Vue 2 迁移到 Vue 3

如果你正在从 Vue2 迁移,主要需要注意事件命名的变化:

// Vue 2 版本
<divv-touch:touchhold="handleHold">
<divv-touch:start="handleStart">

// Vue 3 版本
<divv-touch:hold="handleHold">
<divv-touch:press="handleStart">

💡 实际应用场景

1. 移动端轮播图

Image
<template>
<divclass="carousel"v-touch:swipe.left="nextSlide"v-touch:swipe.right="prevSlide">
<img:src="currentImage"alt="轮播图">
</div>
</template>

2. 图片查看器(支持缩放)

Pic
<template>
<divclass="image-viewer"v-touch:zoom.in="zoomIn"v-touch:zoom.out="zoomOut">
<img:src="imageSrc":style="{ transform: `scale(${zoomLevel})` }"alt="可缩放图片">
</div>
</template>

3. 拖拽排序列表

Image
<template>
<divclass="sortable-list">
<div
v-for="(item, index) in items"
:key="item.id"
v-touch:drag="handleDrag(index)"
v-touch:release="handleRelease"
class="list-item"
    >

      {{ item.name }}
</div>
</div>
</template>

4. 左滑删除(移动端常见交互)

Image
<template>
<divclass="message-list">
<div
v-for="message in messages"
:key="message.id"
v-touch:swipe.left="showDelete(message)"
class="message-item"
    >

      {{ message.content }}
<buttonv-if="message.showDelete" @click="deleteMessage(message)">删除</button>
</div>
</div>
</template>

🎉 总结

vue3-touch-events 为 Vue3 开发者提供了强大而简洁的手势交互解决方案:

  • ✅ 简单易用:声明式语法,学习成本低
  • ✅ 功能丰富:覆盖常见手势交互需求
  • ✅ 跨平台支持:一套代码,多端运行
  • ✅ 性能优秀:事件节流,运行流畅
  • ✅ 维护活跃:持续更新,社区支持

无论你是开发移动端应用、PWA,还是需要桌面端触摸支持的 Web 应用,vue3-touch-events 都能让你的交互开发事半功倍!

📚 立即体验:

npm install vue3-touch-events

让复杂的手势交互开发变得简单有趣!🚀

  • vue3-touch-events:https://github.com/robinrodricks/vue3-touch-events/tree/master

  • vue3-touch-events:https://github.com/jerrybendy/vue-touch-events