Skip to content

现象

H5 在安卓部分机型使用 fixed 布局被软键盘顶上去造成输入框被遮挡

原因

html、body 设置了 height: 100% 的自适应布局后,高度跟随屏幕的可用高度改变而改变导致的。

解决方案

方案一:不使用 fixed 定位,使用替代方案(推荐)

a:使用 position:absolute; overflow-y:scroll;

b:使用 display:flex; overflow-y:scroll;

其中,a方案内容区域的高度通过js控制;b方案内容区域的高度通过flex布局自动排版;

方案二:根据输入框获取焦点与失去焦点事件切换 fixed 定位与 static 定位

也可以根据软键盘是否弹起,是否改变屏幕可用高度,来改变 定位方式 或 显示隐藏 fixed定位元素

js
docmHeight: document.documentElement.clientHeight,  // 默认屏幕高度
showHeight: document.documentElement.clientHeight,   // 实时屏幕高度
hideshow: true // 显示或者隐藏
// 监听
watch:{
  showHeight: function() {
    if(this.docmHeight > this.showHeight) {
        this.hideshow = false
    } else {
        this.hideshow = true
    }
  }
}

最后更新时间: