Skip to content

useResizeObserver

Category
Export Size
671 B
Last Changed
15 minutes ago

报告元素内容或边框框的尺寸变化。

Demo

调整大小以查看更改

用法

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

const el = ref(null)
const text = ref('')

useResizeObserver(el, (entries) => {
  const entry = entries[0]
  const { width, height } = entry.contentRect
  text.value = `width: ${width}, height: ${height}`
})
</script>

<template>
  <div ref="el">
    {{ text }}
  </div>
</template>

Directive Usage

This function also provides a directive version via the @vueuse/components package. Learn more about the usage.

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

const text = ref('')

function onResizeObserver(entries) {
  const [entry] = entries
  const { width, height } = entry.contentRect
  text.value = `width: ${width}, height: ${height}`
}
</script>

<template>
  <div v-resize-observer="onResizeObserver">
    {{ text }}
  </div>
</template>

ResizeObserver MDN

类型声明

显示类型声明
typescript
export interface ResizeObserverSize {
  readonly inlineSize: number
  readonly blockSize: number
}
export interface ResizeObserverEntry {
  readonly target: Element
  readonly contentRect: DOMRectReadOnly
  readonly borderBoxSize: ReadonlyArray<ResizeObserverSize>
  readonly contentBoxSize: ReadonlyArray<ResizeObserverSize>
  readonly devicePixelContentBoxSize: ReadonlyArray<ResizeObserverSize>
}
export type ResizeObserverCallback = (
  entries: ReadonlyArray<ResizeObserverEntry>,
  observer: ResizeObserver,
) => void
export interface UseResizeObserverOptions extends ConfigurableWindow {
  /**
   * 设置观察器将观察哪种盒模型的变化。可能的值有 `content-box`(默认值)、`border-box` 和 `device-pixel-content-box`。
   *
   * @default 'content-box'
   */
  box?: ResizeObserverBoxOptions
}
declare class ResizeObserver {
  constructor(callback: ResizeObserverCallback)
  disconnect(): void
  observe(target: Element, options?: UseResizeObserverOptions): void
  unobserve(target: Element): void
}
/**
 * 监听元素内容或边框盒尺寸的变化
 *
 * @see https://vueuse.org/useResizeObserver
 * @param target
 * @param callback
 * @param options
 */
export declare function useResizeObserver(
  target:
    | MaybeComputedElementRef
    | MaybeComputedElementRef[]
    | MaybeRefOrGetter<MaybeElement[]>,
  callback: ResizeObserverCallback,
  options?: UseResizeObserverOptions,
): {
  isSupported: ComputedRef<boolean>
  stop: () => void
}
export type UseResizeObserverReturn = ReturnType<typeof useResizeObserver>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Anthony Fu
Jelf
Hongkun
青椒肉丝
birdxiao
acyza
ByMykel
vaakian X
karma
Shinigami
Alex Kozack
Jacob Clevenger
Sanxiaozhizi
Antério Vieira
zhong666

变更日志

v11.1.0 on 9/16/2024
044dd - fix: update type (#4218)
v11.0.0-beta.2 on 7/17/2024
d4310 - feat: added vResizeObserver directive (#4008)
1c124 - fix: fix target is Ref Array (#4005)
v10.8.0 on 2/20/2024
48421 - fix: do not watch deep to avoid cicular calls (#3753)
v10.0.0-beta.2 on 3/28/2023
3e187 - feat: support element list (#2841)

Released under the MIT License.