vant/packages/vant/docs/markdown/use-window-size.en-US.md
2022-04-27 17:08:12 +08:00

915 B

useWindowSize

Intro

Get the viewport width and height of the browser window, and update it automatically when the window size changes.

Usage

Basic Usage

import { watch } from 'vue';
import { useWindowSize } from '@vant/use';

export default {
  setup() {
    const { width, height } = useWindowSize();

    console.log(width.value); // -> width of browser window
    console.log(height.value); // -> height of browser window

    watch([width, height], () => {
      console.log('window resized');
    });
  },
};

API

Type Declarations

function useWindowSize(): {
  width: Ref<number>;
  height: Ref<number>;
};

Return Value

Name Description Type
width The width of browser window Ref<number>
height The height of browser window Ref<number>