<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
相关推荐
黑客老陈32 分钟前
新手小白如何挖掘cnvd通用漏洞之存储xss漏洞(利用xss钓鱼)正小安38 分钟前
Vite系列课程 | 11. Vite 配置文件中 CSS 配置(Modules 模块化篇)暴富的Tdy1 小时前
【CryptoJS库AES加密】neeef_se1 小时前
Vue中使用a标签下载静态资源文件(比如excel、pdf等),纯前端操作m0_748235611 小时前
web 渗透学习指南——初学者防入狱篇℘团子এ1 小时前
js和html中,将Excel文件渲染在页面上z千鑫1 小时前
【前端】入门指南:Vue中使用Node.js进行数据库CRUD操作的详细步骤m0_748250742 小时前
Web入门常用标签、属性、属性值