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

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>