Skip to content

useMousePressed

Category
Export Size
679 B
Last Changed
15 minutes ago

鼠标按下状态的响应式状态。在目标元素上触发 mousedown touchstart,在窗口上释放 mouseup mouseleave touchend touchcancel

Demo

pressed: false
sourceType: null
开启追踪

基本用法

js
import { useMousePressed } from '@vueuse/core'

const { pressed } = useMousePressed()

默认情况下启用触摸。要使其仅检测鼠标变化,将 touch 设置为 false

js
const { pressed } = useMousePressed({ touch: false })

要仅捕获特定元素上的 mousedowntouchstart,可以通过传递元素的 ref 来指定 target

vue
<script>
const el = ref(null)

const { pressed } = useMousePressed({ target: el })
</script>

<template>
  <div ref="el">
    仅点击此元素将触发更新。
  </div>
</template>

组件用法

vue
<template>
  <UseMousePressed v-slot="{ pressed }">
    是否按下: {{ pressed }}
  </UseMousePressed>
</template>

类型声明

显示类型声明
typescript
export interface MousePressedOptions extends ConfigurableWindow {
  /**
   * Listen to `touchstart` `touchend` events
   *
   * @default true
   */
  touch?: boolean
  /**
   * Listen to `dragstart` `drop` and `dragend` events
   *
   * @default true
   */
  drag?: boolean
  /**
   * Add event listerners with the `capture` option set to `true`
   * (see [MDN](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#capture))
   *
   * @default false
   */
  capture?: boolean
  /**
   * Initial values
   *
   * @default false
   */
  initialValue?: boolean
  /**
   * Element target to be capture the click
   */
  target?: MaybeComputedElementRef
}
/**
 * 反应性鼠标按下状态。由目标元素上的mousedown touchstart触发,并由窗口上的mouseup mouseleave touchend touchcancel释放。
 *
 * @see https://vueuse.org/useMousePressed
 * @param options
 */
export declare function useMousePressed(options?: MousePressedOptions): {
  pressed: Ref<boolean, boolean>
  sourceType: Ref<UseMouseSourceType, UseMouseSourceType>
}
export type UseMousePressedReturn = ReturnType<typeof useMousePressed>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
wheat
Anthony Fu
Meet you
Chris-Robin Ennen
Jonas Schade
RAX7
ByMykel
vaakian X
MinatoHikari
Marshall Thompson
Shinigami
Alex Kozack

变更日志

v10.7.0 on 12/5/2023
17f97 - fix: change type of element parameter to MaybeComputedElementRef (#3566)
v10.5.0 on 10/7/2023
d5c81 - feat: add capture option (#3392)
v10.1.0 on 4/22/2023
4bb5b - feat(useMouse): support custom event extractor (#2991)
v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)

Released under the MIT License.