useElementBounding
Reactive bounding box of an HTML element
Demo
Resize the box to see changes
Usage
<template>
<div ref="el" />
</template>
<script>
import { ref } from 'vue'
import { useElementBounding } from '@vueuse/core'
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>
<script>
import { ref } from 'vue'
import { useElementBounding } from '@vueuse/core'
export default {
setup() {
const el = ref(null)
const { x, y, top, right, bottom, left, width, height } = useElementBounding(el)
return {
el,
/* ... */
}
}
})
</script>
Component
This function also provides a renderless component version via the@vueuse/components
package. Learn more about the usage. <UseElementBounding v-slot="{ width, height }">
Width: {{ width }}
Height: {{ height }}
</UseElementBounding>
<UseElementBounding v-slot="{ width, height }">
Width: {{ width }}
Height: {{ height }}
</UseElementBounding>
Type Declarations
/**
* Reactive bounding box of an HTML element.
*
* @see https://vueuse.org/useElementBounding
* @param target
*/
export declare function useElementBounding(target: MaybeElementRef): {
height: Ref<number>
bottom: Ref<number>
left: Ref<number>
right: Ref<number>
top: Ref<number>
width: Ref<number>
x: Ref<number>
y: Ref<number>
update: () => void
}
export declare type UseElementBoundingReturn = ReturnType<
typeof useElementBounding
>
/**
* Reactive bounding box of an HTML element.
*
* @see https://vueuse.org/useElementBounding
* @param target
*/
export declare function useElementBounding(target: MaybeElementRef): {
height: Ref<number>
bottom: Ref<number>
left: Ref<number>
right: Ref<number>
top: Ref<number>
width: Ref<number>
x: Ref<number>
y: Ref<number>
update: () => void
}
export declare type UseElementBoundingReturn = ReturnType<
typeof useElementBounding
>
Source
Contributors
Anthony Fu
Jelf
wheat
webfansplz
Ciro Lo Sapio
Shinigami
Alex Kozack