useMouse
Reactive mouse position
Demo
Basic Usage
ts
import { useMouse } from '@vueuse/core'
const { x, y, sourceType } = useMouse()
Touch is enabled by default. To only detect mouse changes, set touch
to false
. The dragover
event is used to track mouse position while dragging.
ts
const { x, y } = useMouse({ touch: false })
Custom Extractor
It's also possible to provide a custom extractor function to get the position from the event.
ts
import type { UseMouseEventExtractor } from '@vueuse/core'
import { useMouse, useParentElement } from '@vueuse/core'
const parentEl = useParentElement()
const extractor: UseMouseEventExtractor = event => (
event instanceof Touch
? null
: [event.offsetX, event.offsetY]
)
const { x, y, sourceType } = useMouse({ target: parentEl, type: extractor })
js
import { useMouse, useParentElement } from '@vueuse/core'
const parentEl = useParentElement()
const extractor = (event) =>
event instanceof Touch ? null : [event.offsetX, event.offsetY]
const { x, y, sourceType } = useMouse({ target: parentEl, type: extractor })
Component Usage
This function also provides a renderless component version via the
@vueuse/components
package. Learn more about the usage.
vue
<template>
<UseMouse v-slot="{ x, y }">
x: {{ x }}
y: {{ y }}
</UseMouse>
</template>
Type Declarations
Show Type Declarations
typescript
export type UseMouseCoordType = "page" | "client" | "screen" | "movement"
export type UseMouseSourceType = "mouse" | "touch" | null
export type UseMouseEventExtractor = (
event: MouseEvent | Touch,
) => [x: number, y: number] | null | undefined
export interface UseMouseOptions
extends ConfigurableWindow,
ConfigurableEventFilter {
/**
* Mouse position based by page, client, screen, or relative to previous position
*
* @default 'page'
*/
type?: UseMouseCoordType | UseMouseEventExtractor
/**
* Listen events on `target` element
*
* @default 'Window'
*/
target?: MaybeRefOrGetter<Window | EventTarget | null | undefined>
/**
* Listen to `touchmove` events
*
* @default true
*/
touch?: boolean
/**
* Listen to `scroll` events on window, only effective on type `page`
*
* @default true
*/
scroll?: boolean
/**
* Reset to initial value when `touchend` event fired
*
* @default false
*/
resetOnTouchEnds?: boolean
/**
* Initial values
*/
initialValue?: Position
}
/**
* Reactive mouse position.
*
* @see https://vueuse.org/useMouse
* @param options
*/
export declare function useMouse(options?: UseMouseOptions): {
x: Ref<number, number>
y: Ref<number, number>
sourceType: Ref<UseMouseSourceType, UseMouseSourceType>
}
export type UseMouseReturn = ReturnType<typeof useMouse>
Source
Contributors
Anthony Fu
青椒肉丝
Jelf
Anthony Fu
Eduardo Luiz Schilling
karukenert
Cats Juice
丶远方
RAX7
François Lévesque
vaakian X
Sergey Danilchenko
lzdFeiFei
Scott Bedard
Marshall Thompson
Shinigami
wheat
Alex Kozack
Antério Vieira
Changelog
v12.0.0-beta.1
on 11/21/2024v11.3.0
on 11/21/2024v10.4.0
on 8/25/2023v10.1.0
on 4/22/2023v10.0.0-beta.4
on 4/13/2023v9.11.0
on 1/17/2023v9.10.0
on 1/3/2023