移动端适配问题

This commit is contained in:
runny committed 2026-05-20 16:02:51 +08:00
1 parent 72642b981e
commit e70e17efd4
9 files changed
+271 -11

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
import { ref, onMounted, onBeforeUnmount, readonly } from 'vue';
const isMobile = ref(false);
let listeners = 0;
const update = () => {
isMobile.value = window.innerHeight > window.innerWidth;
};
const startListening = () => {
if (listeners === 0) {
update();
window.addEventListener('resize', update);
}
listeners++;
};
const stopListening = () => {
listeners--;
if (listeners <= 0) {
listeners = 0;
window.removeEventListener('resize', update);
}
};
export function useIsMobile() {
onMounted(() => startListening());
onBeforeUnmount(() => stopListening());
return { isMobile: readonly(isMobile) };
}