首页 > 语言 > JavaScript > 正文

解决vue-cli单页面手机应用input点击手机端虚拟键盘弹出盖住inp

2024-05-06 15:31:08
字体:
来源:转载
供稿:网友

在用vue-cli脚手架搭建手机H5页面应用的时候,其中一页中部有input,底部有position:absolute;bottom:0的元素,

当点击input框时在安卓手机上出现了:

1 虚拟键盘弹出盖住input

2 底部定位的元素被挤上来

网络上很多关于body设定宽高以及scrolltop的方法都不管用,因为这里是路由页面,根据网上的思路,吊起输入键盘的时候页面的高度是变化的,监听window.onresize,判断是否吊起键盘,然后设定底部模块的隐藏和显示,整个块元素的margintop就可以实现了。

代码如下

 mounted () { this.clientHeight = document.documentElement.clientHeight; const that = this; // 安卓手机键盘吊起挡住输入框 window.onresize = function() {  if(document.documentElement.clientHeight < that.clientHeight) {  // scrollVal为负值  let scrollVal = document.documentElement.clientHeight-that.clientHeight;  $(".alert-main").css("marginTop",scrollVal);  $(".bottom-create").hide();  }else {  $(".alert-main").css("marginTop",0);  $(".bottom-create").show();  }   }; },

今天这个bug 遇到了新问题,同样的华为手机上,当从别的路由吊起输入键盘的时候回到当前路由,

document.documentElement.clientHeight 就变成了减去输入键盘高度的值,

这时需要在页面第一次加载将document.documentElement.clientHeight记录到store中,store中的值不会因为页面重新渲染而改变。

以上这篇解决vue-cli单页面手机应用input点击手机端虚拟键盘弹出盖住input问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持错新站长站。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选