简体   繁体   English

防止键盘向上推底部导航栏

[英]Prevent the keyboard from pushing up the bottom navigation bar

These images resume perfectly the problem I have and the found solution.这些图像完美地还原了我遇到的问题和找到的解决方案。

Actual behavior Expected behavior实际行为预期行为

I thought that using the focus and blur events might be a solution, but it turns out that when I click on the OS back button, as I mentioned in the image, the input stays in the focus state, so what could be the solution?我认为使用 focus 和 blur 事件可能是一个解决方案,但事实证明,当我单击操作系统后退按钮时,正如我在图像中提到的那样,输入停留在焦点 state 中,那么解决方案是什么?

Edited已编辑

 const textfield = document.getElementById("textfield"), bottomNav = document.getElementsByClassName("bottom-nav")[0], viewportHeight = window.innerHeight; textfield.addEventListener("focus", () => { bottomNav.style.top = `${viewportHeight - 56}px`; bottomNav.style.bottom = "auto"; }); textfield.addEventListener("blur", () => { bottomNav.style.top = "auto"; bottomNav.style.bottom = 0; });
 *, *::before, *::after { box-sizing: border-box; } body { margin: 0; }.header, .bottom-nav { position: fixed; z-index: 100; left: 0; right: 0; width: 100%; height: 56px; background-color: red; }.header { top: 0; }.bottom-nav { bottom: 0; }.wrapper { width: 100%; padding: 72px 16px; }.textfield { width: 100%; height: 56px; padding: 0 16px; }
 <header class="header"></header> <main class="wrapper"> <input type="text" id="textfield" class="textfield" /> </main> <nav class="bottom-nav"></nav>

If you add something to make the body scrollable, in Chrome for Android it works like a charm, but in Firefox it breaks, I don't know how it behaves in Safari as I don't have an iPhone, but basically the behavior it has in Chrome is the ideal state.如果你添加一些东西使身体可滚动,在 Chrome 中 Android 它就像一个魅力,但在 Firefox 它打破了,我不知道它在 Safari 中的表现,因为我没有 iPhone,但基本上它的行为在 Chrome 中有理想的 state。

This solution is not bulletproof, but it works the same in both Firefox (105) and Chrome (106) for Android.这个解决方案不是防弹的,但它在 Firefox (105) 和 Android 的 Chrome (106) 中工作相同。

Add this to the CSS and change the JavaScript.:将其添加到 CSS 并更改 JavaScript。:

.bottom-nav--hide {
    top: 100vh;
    bottom: auto;
}
const bottomNav = document.getElementsByClassName("bottom-nav")[0],
    viewportHeight = window.innerHeight;

window.addEventListener("resize", () => {
    let vh = window.innerHeight;
    if (vh < viewportHeight) {
        bottomNav.classList.add("bottom-nav--hide");
    } else {
        bottomNav.classList.remove("bottom-nav--hide");
    }
});

Situations in which it doesn't work.它不起作用的情况。

  • If the user opens the site in portrait and goes to landscape.如果用户以纵向打开网站并转到横向。
  • If the user rescales the window and the height is less than the original height.如果用户重新缩放 window 并且高度小于原始高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM