Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Sự kiện nào xảy ra khi cửa sổ trình duyệt được resize trong JavaScript?

Sự kiện onresize trên window kích hoạt khi kích thước viewport thay đổi. Dùng window.outerWidth/outerHeight để lấy kích thước cửa sổ trình duyệt.

Ví dụ

<!DOCTYPE html>
<html>
  <head>
    <script>
      function resizeFunction() {
        var val = "Chiều rộng cửa sổ=" + window.outerWidth + ", Chiều cao cửa sổ=" + window.outerHeight;
        document.getElementById("test").innerHTML = val;
      }
    </script>
  </head>
  <body onresize="resizeFunction()">
    <p>Thay đổi kích thước cửa sổ trình duyệt và xem kích thước cập nhật.</p>
    <p id="test"></p>
  </body>
</html>