vue 移动端弹出键盘导致页面fixed布局错乱

话不多说,直接上问题图片

这里确认按钮是fixed布局 bottom:0 弹出键盘之后按钮被顶到了键盘上面

网上搜到的解决方案有两种,

一种是监听页面高度(我采用的这种)

一种是监听软键盘事件(ios和安卓实现方式不同,未采用)

下面是实现代码

data() {
    return {
        docmHeight: document.documentElement.clientHeight ||document.body.clientHeight,
        showHeight: document.documentElement.clientHeight ||document.body.clientHeight,
        hideshow:true  //显示或者隐藏footer
    }
  },
watch: {
        //监听显示高度
      showHeight:function() {
          if(this.docmHeight > this.showHeight){
            //隐藏
              this.hideshow=false
          }else{
            //显示
              this.hideshow=true
          }
      }
  },
mounted() {
      //监听事件
      window.onresize = ()=>{
          return(()=>{
              this.showHeight = document.documentElement.clientHeight || document.body.clientHeight;
      })()
      }

  },
<div class="bottom" v-show="hideshow">
    <div class="btn">
      确认操作
    </div>
  </div>

我这里使用的是方法是:当键盘弹出时,将按钮隐藏。如果必须出现按钮的话,可以修改按钮回归到正常的流中。

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
哦,我明白了您的问题。您可以使用移动端的`position: fixed`样式和`bottom`属性来实现在键盘上方显示工具条。具体步骤如下: 1. 在模板中添加工具条的 HTML 结构,并设置样式: ```html <template> <div> <!-- ... --> <div class="tool-bar" v-show="isKeyboardVisible"> <button>按钮1</button> <button>按钮2</button> <!-- ... --> </div> </div> </template> <style> .tool-bar { position: fixed; bottom: 50px; /* 根据实际情况调整 */ left: 0; right: 0; background-color: #fff; border-top: 1px solid #ccc; padding: 10px; z-index: 999; /* 保证工具条在最上层 */ } </style> ``` 2. 监听键盘弹出和收起事件,并在回调函数中更新工具条的显示状态: ```html <template> <div> <!-- ... --> </div> </template> <script> export default { data() { return { isKeyboardVisible: false }; }, mounted() { window.addEventListener('resize', this.handleResize); }, beforeDestroy() { window.removeEventListener('resize', this.handleResize); }, methods: { handleResize() { const windowHeight = window.innerHeight; const bodyHeight = document.body.clientHeight; const isKeyboardVisible = windowHeight < bodyHeight; this.isKeyboardVisible = isKeyboardVisible; } } } </script> ``` 在上面的代码中,我们在组件的`mounted`钩子中添加了一个`resize`事件监听器,并在`handleResize`方法中检查窗口高度是否小于文档高度,以判断键盘是否弹出。如果键盘弹出,我们就将`isKeyboardVisible`属性设置为`true`,从而显示工具条。最后,在组件销毁前,我们需要将监听器移除,以避免内存泄漏。 请注意,由于移动设备的键盘高度可能因设备类型和输入法不同而有所差异,因此您可能需要根据实际情况微调工具条的位置和样式。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值