Skip to content

事件指令

事件指令帮助您更高效地管理 DOM 事件。

v-click-outside

检测元素外部点击。非常适合关闭下拉菜单、模态框和弹出框。

基本用法

vue
<template>
  <div v-click-outside="closeDropdown">
    <button @click="show = !show">切换</button>
    <div v-if="show">下拉菜单内容</div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const show = ref(false)

function closeDropdown() {
  show.value = false
}
</script>

带选项

vue
<template>
  <div v-click-outside="{
    handler: closeDropdown,
    exclude: ['.ignore'],
    events: ['click', 'touchstart']
  }">
    <!-- 内容 -->
  </div>
</template>

API

选项类型默认值描述
handlerFunction-点击外部时的回调
excludeArray[]排除检测的元素
capturebooleantrue使用捕获模式
eventsstring[]['click']监听的事件类型
disabledbooleanfalse禁用指令

v-debounce

防抖事件处理程序,限制执行频率。

基本用法

vue
<template>
  <!-- 默认: 300ms -->
  <input v-debounce="handleInput" />

  <!-- 使用修饰符自定义等待时间 -->
  <input v-debounce:500ms="handleInput" />

  <!-- 使用选项对象 -->
  <input v-debounce="{ handler: handleInput, wait: 500 }" />
</template>

<script setup>
function handleInput(event) {
  console.log('防抖输入:', event.target.value)
}
</script>

API

选项类型默认值描述
handlerFunction-要防抖的函数
waitnumber300等待时间(毫秒)
leadingbooleanfalse在开始边缘调用
trailingbooleantrue在结束边缘调用

v-throttle

节流事件处理程序,限制执行频率。

基本用法

vue
<template>
  <!-- 默认: 300ms -->
  <button v-throttle="handleClick">节流点击</button>

  <!-- 使用修饰符自定义等待时间 -->
  <button v-throttle:1s="handleClick">1秒节流</button>
</template>

<script setup>
function handleClick() {
  console.log('节流点击')
}
</script>

API

选项类型默认值描述
handlerFunction-要节流的函数
waitnumber300等待时间(毫秒)
leadingbooleantrue在开始边缘调用
trailingbooleantrue在结束边缘调用

v-long-press

检测元素上的长按手势。

基本用法

vue
<template>
  <button v-long-press="handleLongPress">长按我</button>
  <button v-long-press="{ handler: handleLongPress, duration: 1000 }">
    1秒长按
  </button>
</template>

<script setup>
function handleLongPress(event) {
  console.log('长按触发!')
}
</script>

API

选项类型默认值描述
handlerFunction-长按触发时的回调
durationnumber500持续时间(毫秒)
distancenumber10取消前的最大移动距离
onStartFunction-按下开始时的回调
onCancelFunction-按下取消时的回调

v-hover

跟踪悬停状态并提供回调和 CSS 类。

基本用法

vue
<template>
  <div v-hover="handleHover">悬停我</div>
  <div v-hover="{ onEnter: handleEnter, onLeave: handleLeave, class: 'is-hovering' }">
    悬停我
  </div>
</template>

<script setup>
function handleHover(isHovering, event) {
  console.log('悬停中:', isHovering)
}
</script>

API

选项类型默认值描述
handlerFunction-悬停状态改变时的回调
onEnterFunction-鼠标进入时的回调
onLeaveFunction-鼠标离开时的回调
classstring'v-hover'悬停时添加的 CSS 类
enterDelaynumber0进入延迟(毫秒)
leaveDelaynumber0离开延迟(毫秒)

v-ripple

为元素添加 Material Design 波纹效果。

基本用法

vue
<template>
  <button v-ripple>点击我</button>
  <button v-ripple="'rgba(255, 255, 255, 0.3)'">自定义颜色</button>
  <button v-ripple="{ color: 'red', duration: 800 }">自定义选项</button>
</template>

API

选项类型默认值描述
colorstring'currentColor'波纹颜色
durationnumber600动画持续时间(毫秒)
disabledbooleanfalse禁用波纹效果

基于 MIT 许可发布