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

Cách sử dụng sự kiện onresize trong JavaScript?

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

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>