html5手机网页开发,中文输入法下软键盘遮挡输入框

最近一直再找工做,也很久没有更新博客。。。。  spa

  以前遇到一个问题,就是在手机上点击input框的时候,发现有的时候就是不自动滑动到相应的位置,怎么兼容这个奇葩的手机呢?code

  其实这类问题,只要懂他们的原理话,就很容易解决了。blog

  原理:当你点在foucs上面计算须要滑动的位置input

  解决方式:博客

// 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); } }) }
相关文章
相关标签/搜索