Skip to content

useElementHover

Category
Export Size
598 B
Last Changed
last month

获取元素的响应式悬停状态。

Demo

用法

vue
<script setup>
import { useElementHover } from '@vueuse/core'

const myHoverableElement = ref()
const isHovered = useElementHover(myHoverableElement)
</script>

<template>
  <button ref="myHoverableElement">
    {{ isHovered }}
  </button>
</template>

指令用法

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 ? '谢谢!' : '悬停我' }}
  </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>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Anthony Fu
webfansplz
Joaquín Sánchez
Jelf
geekreal
cx33
Joe Maylor

变更日志

v11.0.0-beta.2 on 7/17/2024
cf439 - feat: add options to the directive (#3897)
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
v10.0.0-beta.0 on 3/14/2023
c86f2 - fix: window is not defined (#2793)
v9.13.0 on 2/18/2023
23283 - feat: give hook delay properties (#2741)

Released under the MIT License.