跳到主要内容

useWindowSize

分类
导出大小
1.02 kB
上次更改
3 周前

响应式窗口大小

演示

Infinity x Infinity

用法

js
import { useWindowSize } from '@vueuse/core'

const { width, height } = useWindowSize()

组件用法

此函数还通过 @vueuse/components 包提供了一个无渲染组件版本。 了解更多关于用法的信息

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

类型声明

typescript
export interface UseWindowSizeOptions extends ConfigurableWindow {
  initialWidth?: number
  initialHeight?: number
  /**
   * Listen to window `orientationchange` event
   *
   * @default true
   */
  listenOrientation?: boolean
  /**
   * Whether the scrollbar should be included in the width and height
   * Only effective when `type` is `'inner'`
   *
   * @default true
   */
  includeScrollbar?: boolean
  /**
   * Use `window.innerWidth` or `window.outerWidth` or `window.visualViewport`
   * visualViewport documentation from MDN(https://mdn.org.cn/zh-CN/docs/Web/API/VisualViewport)
   * @default 'inner'
   */
  type?: "inner" | "outer" | "visual"
}
/**
 * Reactive window size.
 *
 * @see https://vueuse.org.cn/useWindowSize
 * @param options
 */
export declare function useWindowSize(options?: UseWindowSizeOptions): {
  width: ShallowRef<number, number>
  height: ShallowRef<number, number>
}
export type UseWindowSizeReturn = ReturnType<typeof useWindowSize>

源码

SourceDemoDocs

贡献者

Anthony Fu
Anthony Fu
Jelf
Antério Vieira
IlyaL
Robin
Fernando Fernández
huiliangShen
Al-Khawarizmi
Indrek Ardel
Zhousg
vaakian X
vaakian X
Shinigami
wheat
Alex Kozack
Roman Pavlov

更新日志

v12.4.0 on 1/10/2025
dd316 - feat: use passive event handlers everywhere is possible (#4477)
v12.1.0 on 12/22/2024
e78ef - feat: add type: visual option that leverage visualViewport (#4395)
v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.0.0-beta.2 on 7/17/2024
bfba3 - feat: add ability to get outer sizes of the window (#3999)
v10.0.0-beta.3 on 4/12/2023
4e7b0 - fix!: use media query instead of orientationchange listener (#2919)

在 MIT 许可证下发布。