首页 > 移动平台 > 详细

移动端H5 input输入完成后页面底部留白问题

时间:2019-07-22 18:03:28      阅读:193      评论:0      收藏:0      [点我收藏+]

背景: H5页面在微信上展示,不管是在弹窗上的或者页面在偏底部位置的input输入完成之后点击键盘的完成,页面底部留出一片空白的问题

技术分享图片

出现原因分析
  当键盘抬起时,window.scrollY会从0变到键盘的高度,所以解决办法就是当input失去焦点的时候,将window.scrollY重新设置为点击之前的页面的高度(一般window.scrollTo(0,1000000)是可以解决大多数情况)

解决方案1:
  核心代码:

  let currentY = 0;

  focus(){

    currentY = document.body.scrollTop

    // 下面写你的业务代码

  }

 

  onBlur(){

    window.scrollTo(0,currentY) 

    // 下面写你的业务代码

  }

解决方案2:

  核心代码:

    handleFocus(event) {
      let e = event.currentTarget;
      setTimeout(() => {
        e.scrollIntoView({
          block: ‘start‘,
          behavior: ‘smooth‘
        });
      }, 300);
    }
    handleblur() {
      let e = event.currentTarget;
        setTimeout(() => {
          e.scrollIntoView({
            block: ‘end‘,
            behavior: ‘smooth‘
          });
        }, 300);
      window.scrollTo(0, 0);
    }

 

 解决键盘弹出后挡表单的问题

方法1:

$inputclears指input元素,$btnbox指包裹input的div

$inputclears.on(‘focus‘, function(){
  $btnbox.css(‘position‘, ‘static‘)
}).on(‘blur‘, function(e){
  $btnbox.css(‘position‘, ‘fixed‘)
})

方法2:
window.addEventListener(‘resize‘, function() {
  if (document.activeElement.tagName === ‘INPUT‘  || document.activeElement.tagName === ‘TEXTAREA‘) {
    window.setTimeout(function() {
      if(‘scrollIntoView‘ in document.activeElement) {
        document.activeElement.scrollIntoView();
      } else {
        document.activeElement.scrollIntoViewIfNeeded();
      }
    }, 0);
  }
});


--------------------------------------
本文为博主原创文章,转载请附上博文链接!

移动端H5 input输入完成后页面底部留白问题

原文:https://www.cnblogs.com/xuLessReigns/p/11227137.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!