对于苹果 Safari

检测 viewsualViewportresize 事件, 用两个 viewsualViewport.height 作差得键盘高度.

对于新版本 Chrome

使用 virtualKeyboardApi.

1
2
3
4
5
6
7
8
9
10
if((window.navigator as any).virtualKeyboard) {
const virtualKeyboardApi = (window.navigator as any).virtualKeyboard;
virtualKeyboardApi.overlaysContent = true;
virtualKeyboardApi.addEventListener('geometrychange', (e: any) => {
const ctx = $.mobileEditBar.getContext();
const ref = ctx.ref as unknown as HTMLElement;
const height = virtualKeyboardApi.boundingRect.height ?? 0;
ref.style.display = height > 0 ? 'block' : 'none';
});
}