美文网首页
移动端键盘遮挡input解决方法

移动端键盘遮挡input解决方法

作者: Wg流星 | 来源:发表于2017-12-21 18:00 被阅读50次

在移动开发的时候,经常会遇到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);
    }
});

相关文章

网友评论

      本文标题:移动端键盘遮挡input解决方法

      本文链接:https://www.haomeiwen.com/subject/ruoawxtx.html