JavaScript 中的 onresize 事件有什么用?

htmljavascriptprogramming scripts更新于 2026/1/14 0:12:17

当窗口大小调整时,会触发 resize 事件。使用 JavaScript 中的 window.outerWidth 和 window.outerHeight 事件来获取浏览器大小调整时窗口的大小。

示例

您可以尝试运行以下代码来使用 JavaScript 中的 onresize 事件。

<!DOCTYPE html>
<html>
   <head>
      <script>
         function resizeFunction() {
            var val = "Window Width=" + window.outerWidth + ", Window Height=" + window.outerHeight;
            document.getElementById("test").innerHTML = val;
         }
      </script>
   </head>
   <body onresize="resizeFunction()">
      <p>Resize browser window and check the window (browser window) height and width again.</p>
     
      <p id = "test"> </p>
   </body>
</html>

相关文章


有用资源