移动端安卓系统键盘弹起再收起页面无法滚动问题

6 篇文章 0 订阅
3 篇文章 0 订阅
本文主要讨论了在安卓设备上,当最外层样式设置为100vh且页面内容未充满一屏时,键盘弹起会引发布局异常的问题。作者提供了一个解决方案,即通过监听键盘的弹起和收缩来动态调整高度。具体实现包括监听键盘状态,判断设备是否为安卓,以及根据键盘状态更新页面高度。这个方法有助于保持页面在键盘显示和隐藏时的适配性。
摘要由CSDN通过智能技术生成

首先,要在安卓系统出现这种情况经过我反复测试需要满足两种情况,才会触发:
1、最外层样式高度使用100vh;
2、页面内容高度没有撑满一屏;

解决方式:
通过监听键盘弹起、收缩来更改高度值
html

<div class="wrapper">
    <div class="prob_sugg_detail contain_main" ref="main">
      <van-tabs
        v-model="active"
        scrollspy
        sticky
        type="card"
        background="#E5E6E7"
        color="#ffffff"
        title-active-color="#2F3033"
        class="my_tabs"
      >
     </van-tabs>
   </div>
</div>

js

mounted() {
    this.keyboardListener();
},
// 一定要记得移除监听啊
beforeDestroy() {
    window.removeEventListener("resize", this.judgeKeyboardState);
},
// 监听键盘
    keyboardListener() {
      this.originHeight = document.documentElement.clientHeight || documentElement.body.clientHeight;
      window.addEventListener("resize", this.judgeKeyboardState);
    },
    // 判断是键盘弹起还是收起
    judgeKeyboardState() {
      const resizeHeight = document.documentElement.clientHeight ||
        documentElement.body.clientHeight;
      var u = navigator.userAgent;
      let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; //android终端
      if (isAndroid) {
        if (resizeHeight < this.originHeight) {
          // 键盘弹起
          document.querySelector('.wrapper').setAttribute('style', 'height:100vh;')
        } else {
          // 键盘收起
          this.$refs.toolbar.$refs.search.blur();
          if (this.$el.querySelector(".van-tabs__content").scrollHeight < this.$refs.main.clientHeight) {
          document.querySelector('.wrapper').setAttribute('style', 'height:' + this.originHeight + 'px;')
        } else {
          document.querySelector('.wrapper').setAttribute('style', 'height:100vh;')
        }
        }
      }
    },

参考来源https://www.jianshu.com/p/e0408b1d57ac

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值