liujsim / blog

blog
https://github.com/liujsim/blog/issues
MIT License
1 stars 0 forks source link

HTML 5 页面输入法遮挡问题研究 #11

Open liujsim opened 5 years ago

liujsim commented 5 years ago

暴力方案

页面整体位移

focus

let el = document.querySelector('.home')
el.style.webkitTransform = 'translateY(-50px)'

blur

el.style.webkitTransform = 'translateY(0px)'

适合各种 scroll api 无效时强制解决

liujsim commented 5 years ago

scrollIntoView

setInputPosition (inputArea) {
  let inputObj = document.querySelector('#' + inputArea)
    setTimeout(function () {
      inputObj.scrollIntoView(true)
  }, 500)
}
liujsim commented 5 years ago

weui 解决办法

// .container 设置了 overflow 属性, 导致 Android 手机下输入框获取焦点时, 输入法挡住输入框的 bug
    // 相关 issue: https://github.com/weui/weui/issues/15
    // 解决方法:
    // 0. .container 去掉 overflow 属性, 但此 demo 下会引发别的问题
    // 1. 参考 http://stackoverflow.com/questions/23757345/android-does-not-correctly-scroll-on-input-focus-if-not-body-element
    //    Android 手机下, input 或 textarea 元素聚焦时, 主动滚一把
    if (/Android/gi.test(navigator.userAgent)) {
        window.addEventListener('resize', function () {
            if (document.activeElement.tagName == 'INPUT' || document.activeElement.tagName == 'TEXTAREA') {
                window.setTimeout(function () {
                    document.activeElement.scrollIntoViewIfNeeded();
                }, 0);
            }
        })
    }