跳到主要内容

onLongPress

分类
导出大小
1.04 kB
上次更改
2 个月前

监听元素的长时间按压事件。

函数在选项中提供修饰符

  • stop
  • once
  • prevent
  • capture
  • self

演示

已长按: false

已点击: false

用法

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

const htmlRefHook = useTemplateRef<HTMLElement>('htmlRefHook')
const longPressedHook = shallowRef(false)

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

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

<template>
  <p>Long Pressed: {{ longPressedHook }}</p>

  <button ref="htmlRefHook" class="ml-2 button small">
    Press long
  </button>

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

组件用法

此函数还通过 @vueuse/components 包提供了一个无渲染组件版本。了解更多用法

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

const longPressedComponent = shallowRef(false)

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

<template>
  <p>Long Pressed: {{ longPressedComponent }}</p>

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

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

指令用法

此函数还通过 @vueuse/components 包提供了一个指令版本。了解更多用法

vue
<script setup lang="ts">
import { 
vOnLongPress
} from '@vueuse/components'
import {
shallowRef
} from 'vue'
const
longPressedDirective
=
shallowRef
(false)
function
onLongPressCallbackDirective
(
e
: PointerEvent) {
longPressedDirective
.
value
= true
} function
resetDirective
() {
longPressedDirective
.
value
= false
} </script> <template> <
p
>Long Pressed: {{
longPressedDirective
}}</
p
>
<
button
v-on-long-pr
ess
.
prevent
="
onLongPressCallbackDirective
"
class
="ml-2 button small"
> Press long </
button
>
<
button
v-on-long-pr
ess="
[
onLongPressCallbackDirective
, {
delay
: 1000,
modifiers
: {
stop
: true } }]
"
class
="ml-2 button small"
> Press long (with options) </
button
>
<
button
class
="ml-2 button small" @
click
="
resetDirective
">
Reset </
button
>
</template>

类型声明

显示类型声明
ts
export interface OnLongPressOptions {
  /**
   * Time in ms till `longpress` gets called
   *
   * @default 500
   */
  
delay
?: number | ((
ev
: PointerEvent) => number)
modifiers
?: OnLongPressModifiers
/** * Allowance of moving distance in pixels, * The action will get canceled When moving too far from the pointerdown position. * @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 export type
UseOnLongPressReturn
=
ReturnType
<typeof
onLongPress
>

来源

源代码演示文档

贡献者

Anthony Fu
Anthony Fu
webfansplz
IlyaL
NoiseFan
Vida Xie
James Garbutt
Rainbow
SerKo
Robin
IlyaL
OrbisK
Neil Richter
GojkoGalonja
Doctor Wu
donaldkicksyourass
huiliangShen
Lee Crosby
丶远方
vaakian X
sun0day
Yugang Cao
Mikhailov Nikita
三咲智子
wheat
AllenYu
余小磊

更新日志

e5f74 - feat!: 弃用别名导出,转而使用原始函数名称 (#5009)
8c521 - feat(components)!: 重构组件并使其保持一致 (#4912)
415f3 - feat: 允许函数作为延迟值 (#4979)
v13.4.0
319d8 - feat(shared): 为 setTimeout 类型引入 TimerHandle (#4801)
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
7346a - feat: options.onMouseUp 回调 (#3791)
v10.7.0
0e04a - feat: 添加 distanceThreshold 选项 (#3578)
v10.6.0
8eb0b - feat(onLongClick): 返回停止函数 (#3506)

根据 MIT 许可证发布。