<template>
<van-cell-group>
<van-field v-model="inputText" :disabled="props.disable" autosize type="textarea"
:placeholder="t('app_plz_input_content')" @focus="openKeyboard" @blur="closeKeyboard" />
</van-cell-group>
<!-- 使用KeyboardMask 遮罩层组件 -->
<keyboard-mask :is-visible="isKeyboardVisible" @close="closeKeyboard" />
</template>
<script setup lang="ts">
import { onMounted, reactive, ref, watch } from 'vue'
import KeyboardMask from '@/components/KeyboardMask.vue' // 导入KeyboardMask组件
const isKeyboardVisible = ref(false)
// 添加遮罩层解决键盘弹出,点击空白处关闭键盘,触发页面点击事件
const originalHeight = document.documentElement.clientHeight || document.body.clientHeight
const handleResize = () => {
const resizeHeight = document.documentElement.clientHeight ||
document.body.clientHeight
if (resizeHeight < originalHeight) {
isKeyboardVisible.value = true
} else {
isKeyboardVisible.value = false
}
}
window.addEventListener('resize', handleResize)
const closeKeyboard = () => {
isKeyboardVisible.value = false
}
const openKeyboard = () => {
isKeyboardVisible.value = true
}
</script>
<style scoped lang="less">
</style>
##遮罩组件
<template>
<div class="keyboard-mask" v-show="isShowMask" @click="close"></div>
</template>
<script lang="ts" setup>
import { ref, watch } from 'vue'
const emits = defineEmits(['closeKeyBoardMask'])
const props = defineProps({
isVisible: Boolean
})
const isShowMask = ref<boolean>(false)
watch(
() => props.isVisible,
(newVal) => {
isShowMask.value = newVal
},
{ immediate: true }
)
const close = () => {
emits('closeKeyBoardMask')
}
</script>
<style>
/* 遮罩层样式 */
.keyboard-mask {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0);
/* 半透明黑色背景 */
z-index: 9999;
/* 确保遮罩层在最上层 */
}
</style>
【无标题】H5页面解决点击页面处关闭键盘,触发了页面的事件的问题
gaolei_eit2023-10-11 11:11
相关推荐
你也来冲浪吗7 分钟前
MD编辑器用法讲解小小小小宇10 分钟前
十万字总结所有React hooks(含简单原理)MariaH15 分钟前
MySQL数据库DQLEnjoy102417 分钟前
v8垃圾回收机制zhangbao90s17 分钟前
Tauri 与 Electron 对比:性能、包大小及实际权衡Georgewu17 分钟前
【HarmonyOS 5】敏感信息本地存储详解_Le_23 分钟前
css 小师系列:一种新的影响样式优先级的方式😍wordbaby26 分钟前
从前端视角看 MCP:解锁 LLM 工具调用与结构化交互CodePencil28 分钟前
CSS专题之CSS单位卡仔29 分钟前
破解Vue自定义弹窗销毁的"nextSibling"之谜