useElementBounding
获取 HTML 元素的响应式 边界框
Demo
调整框大小以查看更改
用法
vue
<script>
import { useElementBounding } from '@vueuse/core'
import { ref } from 'vue'
export default {
setup() {
const el = ref(null)
const { x, y, top, right, bottom, left, width, height }
= useElementBounding(el)
return {
el,
/* ... */
}
},
}
</script>
<template>
<div ref="el" />
</template>
组件用法
vue
<template>
<UseElementBounding v-slot="{ width, height }">
Width: {{ width }} Height: {{ height }}
</UseElementBounding>
</template>
类型声明
显示类型声明
typescript
export interface UseElementBoundingOptions {
/**
* 在组件卸载时将值重置为 0
*
* @default true
*/
reset?: boolean
/**
* 监听窗口大小调整事件
*
* @default true
*/
windowResize?: boolean
/**
* 监听窗口滚动事件
*
* @default true
*/
windowScroll?: boolean
/**
* 组件挂载时立即调用更新
*
* @default true
*/
immediate?: boolean
/**
* Timing to recalculate the bounding box
*
* Setting to `next-frame` can be useful when using this together with something like {@link useBreakpoints}
* and therefore the layout (which influences the bounding box of the observed element) is not updated on the current tick.
*
* @default 'sync'
*/
updateTiming?: "sync" | "next-frame"
}
/**
* HTML 元素的响应式边界框。
*
* @see https://vueuse.org/useElementBounding
* @param target
*/
export declare function useElementBounding(
target: MaybeComputedElementRef,
options?: UseElementBoundingOptions,
): {
height: Ref<number, number>
bottom: Ref<number, number>
left: Ref<number, number>
right: Ref<number, number>
top: Ref<number, number>
width: Ref<number, number>
x: Ref<number, number>
y: Ref<number, number>
update: () => void
}
export type UseElementBoundingReturn = ReturnType<typeof useElementBounding>
Source
贡献者
Anthony Fu
丶远方
Jelf
wheat
Anthony Fu
Jonas Schade
huiliangShen
vaakian X
Ducz01
hsyq
Hebilicious
webfansplz
Ciro Lo Sapio
Shinigami
Alex Kozack