在移动开发的时候,经常会遇到input框获得焦点的时候被键盘遮挡,试过很多方法后,终于找到一个最好用的,保存起来以备不时之需:
var clientHeight = document.body.clientHeight;
var _focusElem = null;
document.body.addEventListener("focus", function(e) {
_focusElem = e.target || e.srcElement;
}, true);
window.addEventListener("resize", function() {
if(_focusElem && document.body.clientHeight < clientHeight) {
_focusElem.scrollIntoView(true);
}
});