mirror of
https://github.com/jekip/naive-ui-admin.git
synced 2026-02-13 01:32:27 +08:00
fix Bug or add docs
This commit is contained in:
36
src/hooks/event/useWindowSizeFn.ts
Normal file
36
src/hooks/event/useWindowSizeFn.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { tryOnMounted, tryOnUnmounted } from '@vueuse/core';
|
||||
import { useDebounceFn } from '@vueuse/core';
|
||||
|
||||
interface WindowSizeOptions {
|
||||
once?: boolean;
|
||||
immediate?: boolean;
|
||||
listenerOptions?: AddEventListenerOptions | boolean;
|
||||
}
|
||||
|
||||
export function useWindowSizeFn<T>(fn: Fn<T>, wait = 150, options?: WindowSizeOptions) {
|
||||
let handler = () => {
|
||||
fn();
|
||||
};
|
||||
const handleSize = useDebounceFn(handler, wait);
|
||||
handler = handleSize;
|
||||
|
||||
const start = () => {
|
||||
if (options && options.immediate) {
|
||||
handler();
|
||||
}
|
||||
window.addEventListener('resize', handler);
|
||||
};
|
||||
|
||||
const stop = () => {
|
||||
window.removeEventListener('resize', handler);
|
||||
};
|
||||
|
||||
tryOnMounted(() => {
|
||||
start();
|
||||
});
|
||||
|
||||
tryOnUnmounted(() => {
|
||||
stop();
|
||||
});
|
||||
return [start, stop];
|
||||
}
|
||||
Reference in New Issue
Block a user