参考地址:ios
https://segmentfault.com/a/11...
https://blog.csdn.net/github_...
思路是:父容器fixed定位,bottom为0,left为-100%;flex布局;在其余事件触发输入框出现的时候(例如一个评论的icon),父容器 left值变为0,被软键盘顶上来。git
<div class="ipt-box" :class="{iptfcs:showCipt}"> <input @blur="resetipt()" v-model="commenttxt" ref="ctxt"> <p @click="commentcircle()">发送</p> </div> .ipt-box { position: fixed; left: -100%; bottom: 0; width: 100%; z-index: 9999999; display: flex; align-items: center; &.iptfcs { left: 0; } }
好吧,结果是根本顶不上来!须要多加一层父容器!github
<div class="ipt-box" :class="{iptfcs:showCipt}"> <div class="ipt-box-cont"> <input @blur="resetipt()" v-model="commenttxt" ref="ctxt"> <p @click="commentcircle()">发送</p> </div> </div> .ipt-box { position: fixed; left: -100%; bottom: 0; width: 100%; z-index: 9999999; &.iptfcs { left: 0; } .ipt-box-cont{ width: 100%; display: flex; align-items: center; } }
这样作,终因而顶上来了。点击评论icon,showCipt 设置为true后,this.$refs.ctxt.focus() 聚焦准备书写。可是ios出现点击聚焦不灵敏的状况!须要强制聚焦!web
FastClick.prototype.focus = function(targetElement) { var length; if (deviceIsIOS && targetElement.setSelectionRange && targetElement.type.indexOf('date') !== 0 && targetElement.type !== 'time' && targetElement.type !== 'month') { length = targetElement.value.length; targetElement.focus(); //新增这一行 targetElement.setSelectionRange(length, length); } else { targetElement.focus(); } };
这下点击一次就能够聚焦了,并且通过个人iphone7系统输入法测试,没有出现什么问题,当我切换搜狗输入法后,悲剧又发生了,遮挡一半!!换了xr测试,彻底遮挡!!!!segmentfault
确实大部分 Android 浏览器是没问题的,可是测试在 IOS 上,UC 浏览器配合原生输入法和第三方输入法(好比搜狗输入法),输入框都会被彻底挡住;QQ 浏览器或微信浏览器,配合第三方输入法,输入框会被遮住一半;百度浏览器配合第三方输入法输入框也会被彻底遮住。浏览器
通过筛选,最终选择使用定时器,思路以下:微信
点击触发input-------------在获取焦点(软键盘弹起)前,先将页面的scrollTop值存起来-------
------获取焦点------------判断浏览器类型--------若是是ios,设置定时器,将此时内容的高度值赋值给浏览器当前滚动高度(确保彻底显示)------
-----失去光标--------------判断浏览器类型------若为ios,清除定时器,并设置浏览器当前滚动高度值为一开始键盘未弹起的scrollTop值iphone
//解决第三方软键盘唤起时底部input输入框被遮挡问题 var bfscrolltop = document.body.scrollTop;//获取软键盘唤起前浏览器滚动部分的高度 $("input.inputframe").focus(function(){//在这里‘input.inputframe’是个人底部输入栏的输入框,当它获取焦点时触发事件 interval = setInterval(function(){//设置一个计时器,时间设置与软键盘弹出所需时间相近 document.body.scrollTop = document.body.scrollHeight;//获取焦点后将浏览器内全部内容高度赋给浏览器滚动部分高度 },100) }).blur(function(){//设定输入框失去焦点时的事件 clearInterval(interval);//清除计时器 document.body.scrollTop = bfscrolltop;将软键盘唤起前的浏览器滚动部分高度从新赋给改变后的高度 });
应用在个人代码里面:布局
//点击评论icon,触发软键盘弹起 commentFocus(cid) { let _this = this; _this.scrollerval = document.body.scrollTop; _this.showCipt = true; _this.$refs.ctxt.focus(); if (navigator.userAgent.indexOf('iPhone') > -1||navigator.userAgent.indexOf('iPad') > -1){ _this.timer = setInterval(function () { document.body.scrollTop = document.body.scrollHeight; },1000); } },
//失去光标 resetipt() { let _this = this; _this.showCipt = false; if (navigator.userAgent.indexOf('iPhone') > -1 || navigator.userAgent.indexOf('iPad') > -1){ clearInterval(_this.timer); document.body.scrollTop = _this.scrollerval; } },
固然,把浏览器类型存起来用更好啦!测试
而后,就能够洗洗睡了!!!