useMousePressed
鼠标按下状态的响应式状态。在目标元素上触发 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 })
要仅捕获特定元素上的 mousedown
和 touchstart
,可以通过传递元素的 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
贡献者
Anthony Fu
丶远方
wheat
Anthony Fu
Meet you
Chris-Robin Ennen
Jonas Schade
RAX7
ByMykel
vaakian X
MinatoHikari
Marshall Thompson
Shinigami
Alex Kozack