适配移动端表单软键盘遮挡

This commit is contained in:
2026-07-01 10:52:38 +08:00
parent a98740ac6b
commit ac6008788b
4 changed files with 74 additions and 4 deletions
+22 -1
View File
@@ -2,7 +2,7 @@
<Teleport to="body">
<Transition name="modal-fade">
<div v-if="modelValue" class="modal-mask" @click="close">
<section class="modal-panel" @click.stop>
<section class="modal-panel" @click.stop @focusin.capture="scrollFocusedControlIntoView">
<div class="modal-panel__head">
<h2>{{ title }}</h2>
<button class="modal-panel__close" type="button" :disabled="closeDisabled" @click="close">×</button>
@@ -18,6 +18,8 @@
</template>
<script setup lang="ts">
import { nextTick } from 'vue'
const props = defineProps<{
modelValue: boolean
title: string
@@ -34,4 +36,23 @@ const close = (): void => {
}
emit('update:modelValue', false)
}
const isFormControl = (target: EventTarget | null): target is HTMLElement => {
return target instanceof HTMLElement && target.matches('input, textarea, select')
}
const scrollFocusedControlIntoView = async (event: FocusEvent): Promise<void> => {
if (!isFormControl(event.target)) {
return
}
const target = event.target
await nextTick()
window.setTimeout(() => {
target.scrollIntoView({
block: 'center',
inline: 'nearest',
behavior: 'smooth'
})
}, 260)
}
</script>
+2
View File
@@ -2,10 +2,12 @@ import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
import { installKeyboardInset } from './utils/keyboard-inset'
import './styles/base.css'
const app = createApp(App)
installKeyboardInset()
app.use(createPinia())
app.use(router)
app.mount('#app')
+15 -3
View File
@@ -1,4 +1,5 @@
:root {
--keyboard-inset: 0px;
--page-bg: #f8f5ef;
--card-bg: #fffdf8;
--ink: #26231f;
@@ -94,7 +95,7 @@ a {
width: min(100%, 520px);
min-height: 100vh;
margin: 0 auto;
padding: 14px 12px 28px;
padding: 14px 12px calc(28px + var(--keyboard-inset));
}
.mobile-page--plain {
@@ -350,6 +351,8 @@ a {
padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
background: rgba(248, 245, 239, 0.94);
backdrop-filter: blur(18px);
transform: translateY(calc(var(--keyboard-inset) * -1));
transition: transform 0.18s ease;
}
.fixed-bottom__inner {
@@ -390,6 +393,11 @@ a {
background: rgba(255, 253, 248, 0.96);
box-shadow: 0 -8px 26px rgba(58, 48, 34, 0.07);
backdrop-filter: blur(18px);
transition: transform 0.18s ease;
}
.app-keyboard-active .tabbar {
transform: translateY(calc(100% + env(safe-area-inset-bottom)));
}
.tabbar__item {
@@ -457,17 +465,21 @@ a {
display: flex;
align-items: flex-end;
justify-content: center;
padding: 14px;
padding: 14px 14px calc(14px + var(--keyboard-inset));
background: rgba(38, 35, 31, 0.34);
overflow: hidden;
}
.modal-panel {
width: min(100%, 500px);
max-height: min(84vh, 720px);
max-height: min(84vh, 720px, calc(100vh - 28px - var(--keyboard-inset)));
overflow: auto;
overscroll-behavior: contain;
-webkit-overflow-scrolling: touch;
border-radius: 22px 22px 16px 16px;
background: var(--card-bg);
box-shadow: 0 18px 46px rgba(38, 35, 31, 0.18);
touch-action: pan-y;
}
.modal-panel__head {
@@ -0,0 +1,35 @@
const KEYBOARD_ACTIVE_CLASS = 'app-keyboard-active'
const KEYBOARD_INSET_PROPERTY = '--keyboard-inset'
const MIN_KEYBOARD_INSET = 80
const resolveKeyboardInset = (viewport: VisualViewport): number => {
const viewportBottom = viewport.offsetTop + viewport.height
return Math.max(0, Math.round(window.innerHeight - viewportBottom))
}
const applyKeyboardInset = (value: number): void => {
const inset = value >= MIN_KEYBOARD_INSET ? value : 0
const root = document.documentElement
root.style.setProperty(KEYBOARD_INSET_PROPERTY, `${inset}px`)
root.classList.toggle(KEYBOARD_ACTIVE_CLASS, inset > 0)
}
const refreshKeyboardInset = (): void => {
const viewport = window.visualViewport
if (!viewport) {
applyKeyboardInset(0)
return
}
applyKeyboardInset(resolveKeyboardInset(viewport))
}
export const installKeyboardInset = (): void => {
const viewport = window.visualViewport
if (!viewport) {
return
}
refreshKeyboardInset()
viewport.addEventListener('resize', refreshKeyboardInset)
viewport.addEventListener('scroll', refreshKeyboardInset)
window.addEventListener('orientationchange', refreshKeyboardInset)
}