JavaScript 中 onresize 事件有什麼用?


當視窗大小改變時,則觸發 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>

更新於:2020 年 5 月 21 日

201 次瀏覽

開啟您的職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.