Skip to content

onLongPress

类别
导出体积
675 B
上次更改
2 minutes ago

监听元素上的长按事件。

函数在选项中提供了修饰符

  • stop
  • once
  • prevent
  • capture
  • self

示例

长按: false

点击: false

用法

vue
<script setup lang="ts">
import { onLongPress } from '@vueuse/core'
import { ref } from 'vue'

const htmlRefHook = ref<HTMLElement>()
const longPressedHook = ref(false)

function onLongPressCallbackHook(e: PointerEvent) {
  longPressedHook.value = true
}
function resetHook() {
  longPressedHook.value = false
}

onLongPress(
  htmlRefHook,
  onLongPressCallbackHook,
  {
    modifiers: {
      prevent: true
    }
  }
)
</script>

<template>
  <p>长按状态:{{ longPressedHook }}</p>

  <button ref="htmlRefHook" class="ml-2 button small">
    长按
  </button>

  <button class="ml-2 button small" @click="resetHook">
    重置
  </button>
</template>

组件用法

vue
<script setup lang="ts">
import { OnLongPress } from '@vueuse/components'
import { ref } from 'vue'

const longPressedComponent = ref(false)

function onLongPressCallbackComponent(e: PointerEvent) {
  longPressedComponent.value = true
}
function resetComponent() {
  longPressedComponent.value = false
}
</script>

<template>
  <p>长按状态:{{ longPressedComponent }}</p>

  <OnLongPress
    as="button"
    class="ml-2 button small"
    @trigger="onLongPressCallbackComponent"
  >
    长按
  </OnLongPress>

  <button class="ml-2 button small" @click="resetComponent">
    重置
  </button>
</template>

指令用法

vue
<script setup lang="ts">
import { vOnLongPress } from '@vueuse/components'
import { ref } from 'vue'

const longPressedDirective = ref(false)

function onLongPressCallbackDirective(e: PointerEvent) {
  longPressedDirective.value = true
}
function resetDirective() {
  longPressedDirective.value = false
}
</script>

<template>
  <p>长按状态:{{ longPressedDirective }}</p>

  <button
    v-on-long-press.prevent="onLongPressCallbackDirective"
    class="ml-2 button small"
  >
    长按
  </button>

  <button
    v-on-long-press="[onLongPressCallbackDirective, { delay: 1000, modifiers: { stop: true } }]"
    class="ml-2 button small"
  >
    长按(带选项)
  </button>

  <button class="ml-2 button small" @click="resetDirective">
    重置
  </button>
</template>

类型声明

typescript
export interface OnLongPressOptions {
  /**
   * 长按触发 `longpress` 的时间(毫秒)
   *
   * @default 500
   */
  delay?: number
  modifiers?: OnLongPressModifiers
  /**
   * 允许的移动距离(像素),
   * 当移动距离超过该值时,动作将被取消。
   * @default 10
   */
  distanceThreshold?: number | false
  /**
   * Function called when the ref element is released.
   * @param duration how long the element was pressed in ms
   * @param distance distance from the pointerdown position
   * @param isLongPress whether the action was a long press or not
   */
  onMouseUp?: (duration: number, distance: number, isLongPress: boolean) => void
}
export interface OnLongPressModifiers {
  stop?: boolean
  once?: boolean
  prevent?: boolean
  capture?: boolean
  self?: boolean
}
export declare function onLongPress(
  target: MaybeElementRef,
  handler: (evt: PointerEvent) => void,
  options?: OnLongPressOptions,
): () => void

源码

源码演示文档

贡献者

Anthony Fu
一纸忘忧
Anthony Fu
webfansplz
Neil Richter
GojkoGalonja
Doctor Wu
donaldkicksyourass
huiliangShen
Lee Crosby
丶远方
vaakian X
sun0day
Yugang Cao
Mikhailov Nikita
三咲智子
wheat
AllenYu
余小磊

更新日志

v10.10.0 on 5/27/2024
7346a - feat: options.onMouseUp callback (#3791)
v10.7.0 on 12/5/2023
0e04a - feat: add distanceThreshold option (#3578)
v10.6.0 on 11/9/2023
8eb0b - feat(onLongClick): return stop function (#3506)
v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)