useMouseInElement
与元素相关的鼠标位置的响应式状态
示例
Hover me
x: 0
y: 0
sourceType: null
elementX: 0
elementY: 0
elementPositionX: 0
elementPositionY: 0
elementHeight: 0
elementWidth: 0
isOutside: true
用法
vue
<script setup>
import { useMouseInElement } from '@vueuse/core'
import { ref } from 'vue'
const target = ref(null)
const { x, y, isOutside } = useMouseInElement(target)
</script>
<template>
<div ref="target">
<h1>Hello world</h1>
</div>
</template>
组件用法
vue
<template>
<UseMouseInElement v-slot="{ elementX, elementY, isOutside }">
x: {{ elementX }}
y: {{ elementY }}
是否在外部: {{ isOutside }}
</UseMouseInElement>
</template>
指令使用
vue
<script setup lang="ts">
import { vMouseInElement } from '@vueuse/components'
import { UseMouseSourceType } from '@vueuse/core'
interface MouseInElementType {
x: number
y: number
sourceType: UseMouseSourceType
elementX: number
elementY: number
elementPositionX: number
elementPositionY: number
elementHeight: number
elementWidth: number
isOutside: boolean
}
const options = {
handleOutside: true
}
function onMouseInElement({ x, y, sourceType, elementX, elementY, elementPositionX, elementPositionY, elementHeight, elementWidth, isOutside }: MouseInElementType) {
console.log(x, y, sourceType, elementX, elementY, elementPositionX, elementPositionY, elementHeight, elementWidth, isOutside)
}
</script>
<template>
<textarea v-mouse-in-element="onMouseInElement" />
<!-- with options -->
<textarea v-mouse-in-element="[onMouseInElement, options]" />
</template>
类型声明
typescript
export interface MouseInElementOptions extends UseMouseOptions {
handleOutside?: boolean
}
/**
* 与元素相关的响应式鼠标位置
*
* @see https://vueuse.org/useMouseInElement
* @param target
* @param options
*/
export declare function useMouseInElement(
target?: MaybeElementRef,
options?: MouseInElementOptions,
): {
x: Ref<number, number>
y: Ref<number, number>
sourceType: Ref<UseMouseSourceType, UseMouseSourceType>
elementX: Ref<number, number>
elementY: Ref<number, number>
elementPositionX: Ref<number, number>
elementPositionY: Ref<number, number>
elementHeight: Ref<number, number>
elementWidth: Ref<number, number>
isOutside: Ref<boolean, boolean>
stop: () => void
}
export type UseMouseInElementReturn = ReturnType<typeof useMouseInElement>
源码
贡献者
Anthony Fu
一纸忘忧
wheat
Fernando Fernández
青椒肉丝
Anthony Fu
Jakub Freisler
Coder Poet
Aymoc
Mikhailov Nikita
vaakian X
Jelf
Ivan Agafonov
Shinigami
Alex Kozack
ctholho
Nurettin Kaya
Antério Vieira
更新日志
v12.4.0
on 1/10/2025v12.3.0
on 1/2/2025v12.0.0-beta.1
on 11/21/2024v11.1.0
on 9/16/2024v11.0.0-beta.2
on 7/17/2024v10.7.0
on 12/5/2023v10.6.0
on 11/9/2023v9.12.0
on 1/29/2023v9.11.0
on 1/17/2023