简体   繁体   English

移动键盘打开/关闭时滞后

[英]Lag when mobile keyboard opens/closes

I have a mobile website made using native HTML, CSS and Javascript.我有一个使用原生 HTML、CSS 和 Javascript 制作的移动网站。

Whenever the Mobile Keyboard opens/closes when I tap on an <input> , the site automatically shifts all the elements up/down, in a very laggy manner.每当我点击<input>时移动键盘打开/关闭时,该站点都会以非常滞后的方式自动向上/向下移动所有元素。

Take a look at this GIF I made below:看看我在下面制作的这个 GIF:

滞后的用户界面

Why causes this to be so laggy?为什么导致它如此滞后?

  • My guess is that whenever the website changes viewports, it has to shift a lot of elements which cause the lag.我的猜测是,每当网站更改视口时,它都必须移动很多导致延迟的元素。

Could someone please advice if there's a way to reduce the lag?如果有减少延迟的方法,有人可以请教吗? Is there a way to detect the viewport change, freeze the elements then reset them to their original position without the automatic gradual shift downwards?有没有办法检测视口变化,冻结元素然后将它们重置到原始位置而不自动逐渐向下移动?

@SunAwtCanvas, this was my solution using @capacitor/keyboard to enable the following event listeners. @SunAwtCanvas,这是我使用@capacitor/keyboard 启用以下事件侦听器的解决方案。 To start with, you might want to listen to window events:首先,您可能想要收听窗口事件:

window.addEventListener('keyboardWillShow', () => {
  console.log("Keyboard will Show");
});
window.addEventListener('keyboardDidShow', () => {
  console.log("Keyboard is Shown");
});

Based on these conditions and in order to manually control viewport scroll to the bottom of the screen, you would then add element.scrollIntoView() method, providing it "false" argument, like so: element.scrollIntoView(false);基于这些条件,为了手动控制视口滚动到屏幕底部,您可以添加element.scrollIntoView()方法,为其提供“false”参数,如下所示: element.scrollIntoView(false);

Docs:文档:

  1. element.scrollIntoView() 元素.scrollIntoView()

  2. window.addEventListener('keyboardWillShow') window.addEventListener('keyboardWillShow')

It is possible to reduce the lag and optimize the experience by detecting viewport changes and manually applying layout changes instead of relying on automatic shifting.可以通过检测视口更改和手动应用布局更改而不是依赖自动移动来减少延迟并优化体验。 To do this, you can use JavaScript event listeners that listen for events like window resize, orientation change, or keyboard show/hide to detect a change in the viewport and update the layout accordingly.为此,您可以使用 JavaScript 事件侦听器来侦听窗口大小调整、方向更改或键盘显示/隐藏等事件,以检测视口中的更改并相应地更新布局。 You can also utilize CSS media queries to specify different layouts based on different screen sizes.您还可以利用 CSS 媒体查询根据不同的屏幕尺寸指定不同的布局。 This way your code would be more flexible and optimized for different types of devices.这样您的代码将更加灵活并针对不同类型的设备进行优化。

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

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