vue h5 在安卓手机端input获得焦点后软键盘弹出顶起页面和fixed问题

问题描述
在移动端使用position:fixed布局。当页面中包含input、textarea输入框的时候,或者有调起软键盘的操作时,安卓浏览器下,可视窗口的高度改变,导致页面上的高度重新计算,页面被压扁或者页面被上移或底部fixed定位被顶上去的问题。ios下的浏览器(safari)没有这个问题。

测试问题

点击输入框,输入法弹出,输入框会被顶不见

没弹出软键盘前

 

弹出软键盘后

 

原因
在ios下,软键盘是叠在可视窗口上面的,也就是不影响可视窗口的大小。但是在安卓,软键盘是在窗口中,即占用窗口的面积。

解决方案
理论:给viewport设置height值,可用window.innerHeight赋值。旋转的时候重新设置

js代码

mounted() {
 this.$nextTick(_ => {
  let realHeight =window.innerWidth > window.innerHeight ? window.innerWidth :
   window.innerHeight
  this.setMetaHeight(realHeight)
 })
},
methods: {

 // 设置meta高度 解决安卓浏览器软键盘弹起,占用窗口的面积问题
 setMetaHeight(height) {
	document.head.querySelector("meta[name='viewport']").setAttribute('id', 'viewportMeta')
	let metaEl = document.querySelector('#viewportMeta')
	let content = 'height=' + height + ',width=device-width,initial-scale=1.0,user-      
    scalable=no'
	metaEl.setAttribute('name', 'viewport')
	metaEl.setAttribute('content', content)
	this.$forceUpdate()
 }
}

 解决方案二

mounted() {
 this.$nextTick(_ => {
  const realHeight =window.innerWidth > window.innerHeight ? window.innerWidth :             
   window.innerHeight
  const originalHeight = document.documentElement.clientHeight ||document.body.clientHeight
  window.addEventListener('resize', () => {
  //键盘弹起与隐藏都会引起窗口的高度发生变化
  const resizeHeight = document.documentElement.clientHeight || document.body.clientHeight
  if (resizeHeight - 0 < originalHeight - 0) {
  //当软键盘弹起,在此处操作
  console.log('进入到软键盘弹起=========')
  this.setMetaHeight(realHeight)
  } else {
  //当软键盘收起,在此处操作
  console.log('进入到软键盘收起=========')
  this.setMetaHeight('100%')
    }
   })
 })
},
methods: {

 // 设置meta高度 解决安卓浏览器软键盘弹起,占用窗口的面积问题
 setMetaHeight(height) {
	document.head.querySelector("meta[name='viewport']").setAttribute('id', 'viewportMeta')
	let metaEl = document.querySelector('#viewportMeta')
	let content = 'height=' + height + ',width=device-width,initial-scale=1.0,user-      
    scalable=no'
	metaEl.setAttribute('name', 'viewport')
	metaEl.setAttribute('content', content)
	this.$forceUpdate()
 }
}

附上解决后效果图 

 

注:以上修改后高度不会随着键盘弹起而改变,潜在问题是键盘弹起时会遮盖底部内容 

到此结束

Vue.js开发的H5应用中,有时安卓手机上使用软键盘会覆盖网页内容,导致页面滚动区域被隐藏。这是因为当用户点击输入框时,系统默认会将焦点切换到该输入框,并显示软键盘,这通常会导致页面顶部下。解决这个问题的方法有几种: 1. **手动调整布局**:在页面加载完成后,监听window的resize事件,检查是否需要调整页面的样式,例如通过JavaScript设置body的padding-top等于键盘的高度。 ```javascript new Vue({ mounted() { document.addEventListener('DOMContentLoaded', function () { const keyboardHeight = document.body.scrollHeight - window.innerHeight; if (keyboardHeight > 0) { window.addEventListener('resize', handleResize); } }); function handleResize() { const keyboardHeight = document.body.scrollHeight - window.innerHeight; if (keyboardHeight > 0) { // 更新页面布局,如设置scrollTop、offsetTop等 this.$refs.yourInputEl.offsetTop; // 替换为实际的输入元素ID或引用 } else { window.removeEventListener('resize', handleResize); } } }, }); ``` 2. **使用CSS Hack**:针对某些浏览器,可以尝试使用`@media screen and (-webkit-min-device-pixel-ratio:0)`来调整viewportposition属性,以适应键盘出现的情况。 ```css @media screen and (-webkit-min-device-pixel-ratio:0) { body { padding-bottom: env(safe-area-inset-bottom); // 或者设置具体的键盘高度值 } } ``` 3. **利用Vue自定义指令**:创建一个全局指令,处理键盘显示隐藏时页面的自动调整。 ```javascript Vue.directive('hideKeyboard', { inserted: function (el) { el.addEventListener('focus', function () { // 监听输入框获取焦点时,隐藏其他元素 // 或者直接处理页面滚动 const keyboardHeight = getKeyboardHeight(); if (keyboardHeight > 0) { el.style.position = 'fixed'; el.style.top = keyboardHeight + 'px'; } }); el.addEventListener('blur', function () { // 键盘失去焦点时恢复原状 el.style.position = ''; el.style.top = ''; }); } }); function getKeyboardHeight() { /* 获取键盘高度的方法 */ } ```
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值