mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
docs(@vant/use): add useWindowSize doc
This commit is contained in:
parent
90669560a4
commit
56d4e90cca
@ -30,7 +30,7 @@ module.exports = {
|
||||
'/src/useEventListener/',
|
||||
'/src/usePageVisibility/',
|
||||
'/src/useScrollParent/',
|
||||
// '/src/useWindowSize/',
|
||||
'/src/useWindowSize/',
|
||||
],
|
||||
},
|
||||
],
|
||||
|
43
packages/vant-use/src/useWindowSize/README.md
Normal file
43
packages/vant-use/src/useWindowSize/README.md
Normal file
@ -0,0 +1,43 @@
|
||||
# useWindowSize
|
||||
|
||||
获取浏览器窗口的视口宽度和高度,并在窗口大小变化时自动更新。
|
||||
|
||||
## 代码演示
|
||||
|
||||
### 基本用法
|
||||
|
||||
```js
|
||||
import { watch } from 'vue';
|
||||
import { useWindowSize } from '@vant/use';
|
||||
|
||||
export default {
|
||||
setup() {
|
||||
const { width, height } = useWindowSize();
|
||||
|
||||
console.log(width.value); // -> 窗口宽度
|
||||
console.log(height.value); // -> 窗口高度
|
||||
|
||||
watch([width, height], () => {
|
||||
console.log('window resized');
|
||||
});
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### 类型定义
|
||||
|
||||
```ts
|
||||
function useWindowSize(): {
|
||||
width: Ref<number>;
|
||||
height: Ref<number>;
|
||||
};
|
||||
```
|
||||
|
||||
### 返回值
|
||||
|
||||
| 参数 | 说明 | 类型 |
|
||||
| ------ | -------------- | -------------- |
|
||||
| width | 浏览器窗口宽度 | _Ref\<number>_ |
|
||||
| height | 浏览器窗口高度 | _Ref\<number>_ |
|
@ -2,9 +2,9 @@ import { ref } from 'vue';
|
||||
import { inBrowser } from '../shared';
|
||||
import { useEventListener } from '../useEventListener';
|
||||
|
||||
export function useWindowSize(initialWidth = 0, initialHeight = 0) {
|
||||
const width = ref(inBrowser ? window.innerWidth : initialWidth);
|
||||
const height = ref(inBrowser ? window.innerHeight : initialHeight);
|
||||
export function useWindowSize() {
|
||||
const width = ref(inBrowser ? window.innerWidth : 0);
|
||||
const height = ref(inBrowser ? window.innerHeight : 0);
|
||||
|
||||
const onResize = () => {
|
||||
width.value = window.innerWidth;
|
||||
|
Loading…
x
Reference in New Issue
Block a user