主题
useElementHover
反应式元素的悬停状态。
¥Reactive element's hover state.
示例
用法
¥Usage
vue
<script setup>
import { useElementHover } from '@vueuse/core'
const myHoverableElement = ref()
const isHovered = useElementHover(myHoverableElement)
</script>
<template>
<button ref="myHoverableElement">
{{ isHovered }}
</button>
</template>
指令用法
¥Directive Usage
vue
<script setup lang="ts">
import { vElementHover } from '@vueuse/components'
import { ref } from 'vue'
const isHovered = ref(false)
function onHover(state: boolean) {
isHovered.value = state
}
</script>
<template>
<button v-element-hover="onHover">
{{ isHovered ? 'Thank you!' : 'Hover me' }}
</button>
</template>
你还可以提供悬停选项:
¥You can also provide hover options:
vue
<script setup lang="ts">
import { vElementHover } from '@vueuse/components'
import { ref } from 'vue'
const isHovered = ref(false)
function onHover(hovered: boolean) {
isHovered.value = hovered
}
</script>
<template>
<button v-element-hover="[onHover, { delayEnter: 1000 }]">
<span>{{ isHovered ? 'Thank you!' : 'Hover me' }}</span>
</button>
</template>
类型声明
typescript
export interface UseElementHoverOptions extends ConfigurableWindow {
delayEnter?: number
delayLeave?: number
}
export declare function useElementHover(
el: MaybeRefOrGetter<EventTarget | null | undefined>,
options?: UseElementHoverOptions,
): Ref<boolean>