欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > vue H5解决安卓手机软键盘弹出,页面高度被顶起

vue H5解决安卓手机软键盘弹出,页面高度被顶起

2025/5/13 13:22:41 来源:https://blog.csdn.net/pqn_cc/article/details/147904409  浏览:    关键词:vue H5解决安卓手机软键盘弹出,页面高度被顶起
开发中安卓机上遇到的软键盘弹出导致布局问题
  • 直接上代码_
在这里插入代码片
<div class="container"><div class="appContainer" :style="{height:isKeyboardOpen? Heights :'inherit'}"><p class="name"><!-- 绑定输入框聚焦和失焦事件 --><input class="grow" type="text" v-model="name" @focus="handleInputFocus"@blur="handleInputBlur"/></p></div><!-- 给 footer 添加动态类名 --><div :class="['footer', { 'keyboard-active': isKeyboardOpen }]"><div class="next" @click="nextClick">下一步</div><div class="step next" @click="stepClick">上一步</div></div></div>
在这里插入代码片
data() {return {name:'',isKeyboardOpen: false,originalWindowHeight: window.innerHeight,isAndroid: /Android/i.test(navigator.userAgent),Heights:'inherit'};
}
mounted() {if(this.isAndroid) {// 监听窗口大小变化window.addEventListener("resize", this.handleWindowResize);}
},
beforeDestroy() {// 移除监听事件,防止内存泄漏if (this.isAndroid) {        window.removeEventListener("resize", this.handleWindowResize);}
},
methods: {stepClick(){console.log('上一步');},nextClick(){ if(!this.name){Toast("请输入姓名");return;}console.log('下一步');},handleInputFocus() {if (this.isAndroid) {// 输入框聚焦时触发窗口大小检查this.handleWindowResize();}},handleInputBlur() {if (this.isAndroid) {// 输入框失焦时触发窗口大小检查this.handleWindowResize();}},handleWindowResize() {if (this.isAndroid) {const currentWindowHeight = window.innerHeight;this.Heights=currentWindowHeight + 'px';                this.isKeyboardOpen = currentWindowHeight < this.originalWindowHeight;// 通过比较窗口高度判断键盘是否打开}}
}
<style>// 键盘弹出时隐藏 footer.footer.keyboard-active {// display: none;//这一种写法调起键盘时有闪烁问题position: relative;//一种方法bottom: 0;}
</style>

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词