当前位置: 首页 > ops >正文

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>

在这里插入图片描述

http://www.xdnf.cn/news/5694.html

相关文章:

  • 连接词化归律详解
  • Linux系统管理与编程19:自动部署dns
  • 十二、操作符重载
  • 多媒体预研
  • 基于STM32、HAL库的BME680气压传感器 驱动程序设计
  • 如何找正常运行虚拟机
  • 【Kubernetes】初识基础理论(第一篇)
  • OSCP - Hack The Box - Sau
  • 【面试真题】王者荣耀亿级排行榜,如何设计?
  • 项目售后服务承诺书,软件售后服务方案,软件安装文档,操作文档,维护文档(Word原件)
  • MySQL性能调优探秘:我的实战笔记 (下篇:深入内核、锁与监控)
  • java反序列化commons-collections链1
  • 如何使用 WMIC 命令在 Windows 11 或 10 上卸载软件
  • 每周靶点分享:CD20、GIPR及文献分享
  • el-table滚动条,都悬浮在页面的底层显示,表格吸底滚动条效果
  • bootstrap自助(抽样)法
  • 机器学习实战:归一化与标准化的选择指南
  • Spring Boot 参数验证
  • 【TTS学习笔记】:语音合成领域基本术语
  • 关系型数据库和非关系型数据库
  • Redis数据类型
  • 结合 GWAS 和 TWAS 鉴定玉米籽粒中生育色醇水平的候选致病基因
  • Java Spring Boot项目目录规范示例
  • 【Python训练营打卡】day23 @浙大疏锦行
  • 基于Backtrader库的实时回测与模拟交易策略实战
  • day06_java中的流程控制语句
  • Milvus 2.4 使用详解:从零构建向量数据库并实现搜索功能(Python 实战)
  • 力扣-138.随机链表的复制
  • js fetch流式请求 AI动态生成文本,实现逐字生成渲染效果
  • 个人博客系统测试报告