移动端键盘弹出时候,导致body高度变低,背景图上移

  <div class="bg_box" id="bg_box" style="width: 100%;position:absolute;top: 0;z-index: -1;">
    <div class="bg1"></div>
    <div class="bg2"></div>
  </div>

body {
  width: 100%;
  height: 100vh;
  background-color: #fefdfd;
  overflow: hidden;
}
.bg1 {
  width: 100%;
  background: url(../images/login_banner_02.png) center center/100% 100% no-repeat;
  height: 5.04rem;
  position: absolute;
  top: 0;
  z-index: -1;
}

.bg2 {
  height: 7.88rem;
  position: absolute;
  bottom: 0;
  z-index: -1;
  width: 100%;
  background: url(../images/login_banner_01.png) center center/100% 100% no-repeat;
}
<script>
  $(function () {
    var height = document.documentElement.clientHeight
      $("#bg_box").height(height)
  })
</script>

键盘弹出时,body,html的高度由原来的100% 变成了 100%-键盘高度,所以bg2 会上移。
解决办法是给bg1 和 bg2 盒子套一个父级,绝对定位,将它的高度设置为视口高度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
哦,我明白了您的问题。您可以使用移动端的`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`,从而显示工具条。最后,在组件销毁前,我们需要将监听器移除,以避免内存泄漏。 请注意,由于移动设备的键盘高度可能因设备类型和输入法不同而有所差异,因此您可能需要根据实际情况微调工具条的位置和样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗倩楠_666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值