Skip to content

useMousePressed

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

¥Reactive mouse pressing state. Triggered by mousedown touchstart on target element and released by mouseup mouseleave touchend touchcancel on window.

示例

pressed: false
sourceType: null
Tracking on

基本用法

¥Basic Usage

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

const { pressed } = useMousePressed()

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

¥Touching is enabled by default. To make it only detects mouse changes, set touch to false

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

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

¥To only capture mousedown and touchstart on specific element, you can specify target by passing a ref of the element.

vue
<script setup>
const el = ref(null)

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

<template>
  <div ref="el">
    Only clicking on this element will trigger the update.
  </div>
</template>

组件用法

¥Component Usage

vue
<template>
  <UseMousePressed v-slot="{ pressed }">
    Is 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
}
/**
 * Reactive mouse pressing state.
 *
 * @see https://vueuse.org/useMousePressed
 * @param options
 */
export declare function useMousePressed(options?: MousePressedOptions): {
  pressed: Ref<boolean>
  sourceType: Ref<UseMouseSourceType>
}
export type UseMousePressedReturn = ReturnType<typeof useMousePressed>

源代码

源代码示例文档

变更日志

No recent changes

VueUse 中文网 - 粤ICP备13048890号