跳到内容

useElementBounding

分类
导出大小
1.25 kB
最近更改
3 weeks ago

HTML 元素的响应式 边界框

演示

调整盒子大小以查看变化

用法

vue
<script>
import { useElementBounding } from '@vueuse/core'
import { useTemplateRef } from 'vue'

export default {
  setup() {
    const el = useTemplateRef('el')
    const { x, y, top, right, bottom, left, width, height }
        = useElementBounding(el)

    return {
      el,
      /* ... */
    }
  },
}
</script>

<template>
  <div ref="el" />
</template>

组件用法

此函数还通过 @vueuse/components 包提供了无渲染组件版本。 了解更多用法

vue
<template>
  <UseElementBounding v-slot="{ width, height }">
    Width: {{ width }} Height: {{ height }}
  </UseElementBounding>
</template>

指令用法

此函数还通过 @vueuse/components 包提供了指令版本。 了解更多用法

vue
<script setup lang="ts">
import { vElementBounding } from '@vueuse/components'

interface BoundingType {
  height: number
  bottom: number
  left: number
  right: number
  top: number
  width: number
  x: number
  y: number
}

function onBounding({ height, bottom, left, right, top, width, x, y }: BoundingType) {
  console.log(height, bottom, left, right, top, width, x, y)
}

const options = {
  reset: true,
  windowResize: true,
  windowScroll: true,
  immediate: true,
  updateTiming: 'sync',
}
</script>

<template>
  <textarea v-element-bounding="onBounding" />
  <!-- with options -->
  <textarea v-element-bounding="[onBounding, options]" />
</template>

类型声明

显示类型声明
typescript
export interface UseElementBoundingOptions {
  /**
   * Reset values to 0 on component unmounted
   *
   * @default true
   */
  reset?: boolean
  /**
   * Listen to window resize event
   *
   * @default true
   */
  windowResize?: boolean
  /**
   * Listen to window scroll event
   *
   * @default true
   */
  windowScroll?: boolean
  /**
   * Immediately call update on component mounted
   *
   * @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"
}
/**
 * Reactive bounding box of an HTML element.
 *
 * @see https://vueuse.org.cn/useElementBounding
 * @param target
 */
export declare function useElementBounding(
  target: MaybeComputedElementRef,
  options?: UseElementBoundingOptions,
): {
  height: ShallowRef<number, number>
  bottom: ShallowRef<number, number>
  left: ShallowRef<number, number>
  right: ShallowRef<number, number>
  top: ShallowRef<number, number>
  width: ShallowRef<number, number>
  x: ShallowRef<number, number>
  y: ShallowRef<number, number>
  update: () => void
}
export type UseElementBoundingReturn = ReturnType<typeof useElementBounding>

源码

SourceDemoDocs

贡献者

Anthony Fu
Anthony Fu
Jelf
wheat
IlyaL
青椒肉丝
Robin
Jonas Schade
huiliangShen
vaakian X
Ducz01
hsyq
Hebilicious
webfansplz
Ciro Lo Sapio
Shinigami
Alex Kozack

更新日志

v12.3.0 on 1/2/2025
67a9c - feat: 为 vElementBounding 添加指令 (#4436)
v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: 移除 Vue 2 支持,优化包并清理代码 (#4349)
v11.0.0-beta.2 on 7/17/2024
0fa17 - feat: 添加 updateTiming 选项 (#3869)
v10.7.1 on 12/27/2023
70dbd - fix: 通过 css 或 style 触发 (#3664)

在 MIT 许可证下发布。