h5 键盘遮住输入框

被刻印的时光 ゝ 提交于 2019-12-10 13:49:50
var index = 0;
if (isIOS()) {
    document.body.addEventListener('focusin', (e) => {
        //软键盘弹出的事件处理
        index = index + 1;
    });
    document.body.addEventListener('focusout', (e) => {
        //软键盘收起的事件处理
        window.setTimeout(() => {
            index = index + 1;
            if (index%2 === 0) {
                $(".header-left")[0].scrollIntoViewIfNeeded();
            }
        }, 100);
    });
} else {
    var originalHeight=document.documentElement.clientHeight ||document.body.clientHeight;
    window.οnresize=function(){
        //键盘弹起与隐藏都会引起窗口的高度发生变化
        var resizeHeight=document.documentElement.clientHeight || document.body.clientHeight;
        if(resizeHeight-0<originalHeight-0){
            window.setTimeout(() => {
                document.activeElement.scrollIntoViewIfNeeded();
            }, 100);
        }else{
            //当软键盘收起,在此处操作
            window.setTimeout(() => {
                $(".header-left")[0].scrollIntoViewIfNeeded();
            }, 100);
        }
    }
}

// 判断是否为ios
function isIOS() {
    var u = navigator.userAgent, app = navigator.appVersion;
    return !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!