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

2019-05-21  本文已影响0人  w候人兮猗

说明

最近在用vue写几个H5页面在微信上展示,遇到一个在弹窗上input输入完成之后点击键盘的完成,页面底部留出一片空白的问题

image

出现原因分析

解决

<template>
  <input class="m-input" :value="value" @input="$emit('input', $event.target.value)" @focus="inputFocus()" @focusout="inputFocusOut">
</template>

<script>
  export default {
    name: "MInput",
    props:['value'],
    data(){
      return{
        timer:null
      }
    },
    methods:{
      inputFocus(){
        if(this.timer){
          clearTimeout(this.timer)
        }
      },
      inputFocusOut(){
        this.timer = setTimeout(() => {
          window.scrollTo(0,0)
        },10)
      }
    },
    destroyed(){
      if(this.timer){
        clearTimeout(this.timer)
      }
    }
  }
</script>

补充:解决方案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);
}

补充:解决方案3

 //解决键盘弹出后挡表单的问题
    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);
      }
    });

补充:页面来回弹跳

handleFocus(event) {
    clearTimeout(this.timer);
}
handleblur() {
    this.timer = setTimeout(() => {
        document.body.scrollTop = 0;
        window.pageXOffset = 0;
        document.documentElement.scrollTop = 0;
    }, 100);
}

最后

上一篇 下一篇

猜你喜欢

热点阅读