JavaScript 中將 iframe 設定為全屏?


以下是 JavaScript 中將 iframe 設定為全屏的程式碼 −

示例

 現場演示

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .result {
      font-weight: 500;
      font-size: 18px;
      color: blueviolet;
   }
   .fullScreen {
      width: 100vw;
      height: 100vh;
   }
</style>
</head>
<body>
<h1>Setting full screen iframe</h1>
<iframe class="frame" src="https://www.wikipedia.com" width="300px" height="200px"
></iframe>
<button class="Btn" style="margin: 15px;">Fullscreen</button>
<h3>Click on the above button to make the iframe go fullscreen</h3>
<script>
   let BtnEle = document.querySelector(".Btn");
   let frameEle = document.querySelector(".frame");
   BtnEle.addEventListener("click", () => {
      frameEle.className = "fullScreen";
   });
</script>
</body>
</html>

輸出

單擊“全屏”按鈕 −

更新時間:2020 年 7 月 16 日

2K+ 次瀏覽

開啟你的 職業

完成課程,即可獲得認證

開始
廣告