mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(use): add useWindowSize
This commit is contained in:
parent
ea846033f7
commit
4fa602bc77
@ -1,4 +1,5 @@
|
|||||||
export { useToggle } from './useToggle';
|
export { useToggle } from './useToggle';
|
||||||
export { useClickAway } from './useClickAway';
|
export { useClickAway } from './useClickAway';
|
||||||
|
export { useWindowSize } from './useWindowSize';
|
||||||
export { useScrollParent } from './useScrollParent';
|
export { useScrollParent } from './useScrollParent';
|
||||||
export { useEventListener } from './useEventListener';
|
export { useEventListener } from './useEventListener';
|
||||||
|
18
packages/vant-use/src/useWindowSize/index.ts
Normal file
18
packages/vant-use/src/useWindowSize/index.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
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);
|
||||||
|
|
||||||
|
const onResize = () => {
|
||||||
|
width.value = window.innerWidth;
|
||||||
|
height.value = window.innerHeight;
|
||||||
|
};
|
||||||
|
|
||||||
|
useEventListener('resize', onResize);
|
||||||
|
useEventListener('orientationchange', onResize);
|
||||||
|
|
||||||
|
return { width, height };
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user