當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


HTML DOM fullscreenEnabled()用法及代碼示例

HTML DOM fullscreenEnabled() 方法用於指定當前文檔是否可以使用全屏模式。它的返回類型是布爾值並且是隻讀屬性。如果全屏模式可用,則返回真,否則返回假。不同的前綴用於使其與您的瀏覽器一起使用。

用法

以下是 fullscreenEnabled() 方法的語法 -

document.fullscreenEnabled()

讓我們看一個 fullscreenEnabled() 方法的例子 -

注意- 您需要使用瀏覽器前綴才能使用 fullscreenEnabled() 方法。檢查最後的注釋以獲取瀏覽器特定的前綴。

示例

讓我們看一個例子 -

<!DOCTYPE html>
<html>
<body>
<h1>fullscreenEnabled() method</h1>
<p>Click the below button to know if this document can be viewed in full screen mode or not</p>
<button onclick="EnableFullScreen();">CHECK</button>
<p id="Sample"></p>
<script>
   function EnableFullScreen() {
      var FullS=(document.fullscreenEnabled || /* Standard syntax */
      document.webkitFullscreenEnabled) /* Chrome, Safari and Opera */
      if(FullS==true)
         document.getElementById("Sample").innerHTML="This document can be viewed in fullscreen mode.";
      else
         document.getElementById("Sample").innerHTML="This document cannot be viewed in fullscreen mode.";
   }
</script>
</body>
</html>

輸出

這將產生以下輸出 -

單擊“檢查”按鈕 -

在上麵的例子中 -

我們首先創建了一個按鈕 CHECK,它將在用戶單擊時執行 EnableFullScreen() 方法。

<button onclick="EnableFullScreen();">CHECK</button>

EnableFullScreen() 函數獲取 document.FullScreenEnabled 屬性值並分配給變量 FullS。返回值可以是真或假。然後使用其innerHTML 屬性將其顯示在id 為“Sample” 的段落中。

function EnableFullScreen() {
   var FullS=(document.fullscreenEnabled || /* Standard syntax */
   document.webkitFullscreenEnabled) /* Chrome, Safari and Opera */
   if(FullS==true)
      document.getElementById("Sample").innerHTML="This document can be viewed in fullscreen mode.";
   else
      document.getElementById("Sample").innerHTML="This document cannot be viewed in fullscreen mode.";
}

注意- 我們已經為 Chrome、Safari 和 Opera 瀏覽器指定了 fullscreenEnabled() 標準語法。如果您使用任何其他瀏覽器,請使用以下前綴。

  • 火狐:document.mozFullScreenEnabled
  • IE/Edge:document.msFullscreenEnabled

相關用法


注:本文由純淨天空篩選整理自AmitDiwan大神的英文原創作品 HTML DOM fullscreenEnabled() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。