跳到主要內容

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://developer.mozilla.org/zh-CN/docs/Web/API/VisualViewport)
   * @default 'inner'
   */
  type?: "inner" | "outer" | "visual"
}
/**
 * Reactive window size.
 *
 * @see https://vueuse.dev.org.tw/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: 盡可能在任何地方使用被動事件處理器 (#4477)
v12.1.0 on 12/22/2024
e78ef - feat: 新增 type: visual 選項以利用 visualViewport (#4395)
v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: 移除 Vue 2 支援,最佳化套件並清理 (#4349)
v11.0.0-beta.2 on 7/17/2024
bfba3 - feat: 新增取得視窗外部尺寸的功能 (#3999)
v10.0.0-beta.3 on 4/12/2023
4e7b0 - fix!: 使用媒體查詢取代 orientationchange 監聽器 (#2919)

在 MIT 許可證下發布。