Skip to content

useMouseInElement

与元素相关的反应式鼠标位置

¥Reactive mouse position related to an element

示例

Hover me
x: 0
y: 0
sourceType: null
elementX: 0
elementY: 0
elementPositionX: 0
elementPositionY: 0
elementHeight: 0
elementWidth: 0
isOutside: true

用法

¥Usage

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>

组件用法

¥Component Usage

vue
<template>
  <UseMouseInElement v-slot="{ elementX, elementY, isOutside }">
    x: {{ elementX }}
    y: {{ elementY }}
    Is Outside: {{ isOutside }}
  </UseMouseInElement>
</template>

类型声明

typescript
export interface MouseInElementOptions extends UseMouseOptions {
  handleOutside?: boolean
}
/**
 * Reactive mouse position related to an element.
 *
 * @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>

源代码

源代码示例文档

变更日志

No recent changes

VueUse 中文网 - 粤ICP备13048890号