在安卓手机上,如果底部有一个定位的元素 比如一个input 或者一个 footer 在正常情况下是没有问题的,
但是一旦input获得焦点,那么定位元素会被软键盘向上推,ios手机没有问题
此时解决办法如下:
先判断手机类型,因为如果不判断的话 ios会被影响。
window.onload = function () {
var u = navigator.userAgent;
if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {//安卓手机
// 拿到获取焦点的input
let input = document.querySelector('input')
input.addEventListener('focus', function () {
setInterval(function () {
// 核心
input.scrollIntoView(false);
}, 200)
})
}
}
vue h5移动端Ios软键盘遮挡input输入框导致页面不能自动向上滚动的bug
关键代码
window.addEventListener('resize', function() {
if (
document.activeElement.tagName === 'INPUT' ||
document.activeElement.tagName === 'TEXTAREA'
) {
window.setTimeout(function() {
if ('scrollIntoView' in document.activeElement) {
document.activeElement.scrollIntoView(false)
} else {
document.activeElement.scrollIntoViewIfNeeded(false)
}
}, 0)
}
})
vue--ios手机input点击手机输入键盘顶起页面解决方案
<input type="text" placeholder="请填写手机号" maxlength="11" v-model="phone" @focus="getFocus" />
getFocus() {
$("input").on("blur", function() {
window.scroll(0, 0);
});
},