VirtualKeyboard: geometrychange event

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Sự kiện geometrychange của giao diện VirtualKeyboard được kích hoạt khi bàn phím ảo trên màn hình được bật/tắt giữa trạng thái hiển thị và ẩn.

Sự kiện geometrychange hữu ích để phát hiện khi bàn phím ảo xuất hiện và biến mất, để bạn có thể điều chỉnh bố cục cho phù hợp. Điều này cần thiết khi sử dụng Virtual Keyboard API để vô hiệu hóa việc tự động thay đổi kích thước viewport mặc định của trình duyệt khi bàn phím ảo được hiển thị và ẩn.

Cú pháp

Sử dụng tên sự kiện trong các phương thức như addEventListener(), hoặc đặt thuộc tính trình xử lý sự kiện.

js
addEventListener("geometrychange", (event) => { })

ongeometrychange = (event) => { }

Kiểu sự kiện

Một Event chung.

Ví dụ

Đoạn code sau sử dụng sự kiện geometrychange để phát hiện khi hình học của bàn phím ảo thay đổi, sau đó truy cập thuộc tính boundingRect để truy vấn kích thước và vị trí của bàn phím ảo:

js
if ("virtualKeyboard" in navigator) {
  navigator.virtualKeyboard.overlaysContent = true;

  navigator.virtualKeyboard.addEventListener("geometrychange", (event) => {
    const { x, y, width, height } = event.target.boundingRect;
  });
}

Thông số kỹ thuật

Specification
VirtualKeyboard API
# dom-virtualkeyboard-ongeometrychange

Tương thích trình duyệt

Xem thêm