常见兼容问题处理
1.ios键盘唤起,键盘收起以后页面不归位解决
- 现状:输入内容,软键盘弹出,页面内容整体上移,但是键盘收起,页面内容不下滑
- 原因:固定定位的元素 在元素内 input 框聚焦的时候 弹出的软键盘占位 失去焦点的时候软键盘消失 但是还是占位的 导致input框不能再次输入 在失去焦点的时候给一个事件
- 解决方法:
1
2
3
4
5
6
7
8
9
10
11
12
13<input @blur.prevent="blur()"/>
<script>
blur(){
let u = navigator.userAgent;
let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
if(isIOS){
setTimeout(() => {
const scrollHeight = document.documentElement.scrollTop || document.body.scrollTop || 0
window.scrollTo(0, Math.max(scrollHeight - 1, 0))
}, 200)
}
}
</script>
2.安卓弹出的键盘遮盖文本框
- 现状:安卓微信H5弹出软键盘后挡住input输入框
- 原因:因为调用安卓键盘有一点迟钝,导致如果不延时处理的话,滚动就失效了
- 解决方法:
1
2
3
4
5
6
7
8
9
10
11
12
13<input @focus="focus()"/>
<script>
focus(){
let u = navigator.userAgent;
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
if(isAndroid){
setTimeout(function() {
document.activeElement.scrollIntoViewIfNeeded();
document.activeElement.scrollIntoView();
}, 500);
}
}
</script>Element.scrollIntoView()方法让当前的元素滚动到浏览器窗口的可视区域内。而Element.scrollIntoViewIfNeeded()方法也是用来将不在浏览器窗口的可见区域内的元素滚动到浏览器窗口的可见区域。但如果该元素已经在浏览器窗口的可见区域内,则不会发生滚动