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


HTML fullscreenchange事件用法及代碼示例


當用戶以全屏模式查看元素時,發生全屏更改事件。element.requestFullscreen()方法用於查看全屏模式,而element.exitFullscreen()方法用於取消全屏模式。

用法:

  • 在HTML中:
    <element onfullscreenchange="myScript">
  • 在JavaScript中:
    object.onfullscreenchange = function(){myScript};
  • 在JavaScript中,使用addEventListener()方法:
    object.addEventListener("fullscreenchange", myScript);

以下示例說明了HTML DOM中的fullscreenchange事件:


  • 範例1:本示例說明HTML DOM fullscreenchange事件。
    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <meta name="viewport" 
        content="width=device-width, initial-scale=1"> 
    </head> 
      
    <body onfullscreenchange="function()"> 
        <center> 
        <h1 id="geeks" style="color:green;"> 
                                    GeeksforGeeks</h1> 
        <h3>Fullscreen with JavaScript</h3> 
        <p>Click on the button to open this 
                          page in fullscreen mode.</p> 
        <button onclick="openFullscreen();"> 
                              Fullscreen Mode</button> 
        <button onclick="closeFullscreen();"> 
               Close Fullscreen or pres "Esc"</button> 
          
        <script> 
            var gfg = document.documentElement; 
            function openFullscreen() { 
                if (gfg.requestFullscreen) { 
                    gfg.requestFullscreen(); 
                } 
            } 
            function closeFullscreen() { 
                if (document.exitFullscreen) { 
                    document.exitFullscreen(); 
                }  
            } 
              
        </script> 
    </body> 
      
    </html>                    

    輸出:

  • 範例2:本示例將JavaScript用於fullscreenchange事件。
    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <meta name="viewport" 
        content="width=device-width, initial-scale=1"> 
    </head> 
      
    <body> 
        <center> 
        <h1 id="geeks" style="color:green;"> 
                                    GeeksforGeeks</h1> 
        <h3>Fullscreen with JavaScript</h3> 
        <p>Click on the button to open this 
                          page in fullscreen mode.</p> 
        <button onclick="openFullscreen();"> 
                              Fullscreen Mode</button> 
        <button onclick="closeFullscreen();"> 
               Close Fullscreen or pres "Esc"</button> 
      
        <script> 
            var gfg = document.documentElement; 
            function openFullscreen() { 
                if (gfg.requestFullscreen) { 
                    gfg.requestFullscreen(); 
                } 
            } 
            function closeFullscreen() { 
                if (document.exitFullscreen) { 
                    document.exitFullscreen(); 
                }  
            } 
            var output = document.getElementById("geeks"); 
            document.onfullscreenchange (function() { 
            }); 
        </script> 
    </body> 
      
    </html>                    

    輸出:

  • 範例3:此示例對全屏更改事件使用addEventListener()方法。
    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <meta name="viewport" 
        content="width=device-width, initial-scale=1"> 
    </head> 
      
    <body> 
        <center> 
        <h1 id="geeks" style="color:green;"> 
                                   GeeksforGeeks</h1> 
        <h3>Fullscreen with JavaScript</h3> 
        <p>Click on the button to open this  
                         page in fullscreen mode.</p> 
        <button onclick="openFullscreen();"> 
                             Fullscreen Mode</button> 
        <button onclick="closeFullscreen();"> 
              Close Fullscreen or pres "Esc"</button> 
          
        <script> 
            var gfg = document.documentElement; 
            function openFullscreen() { 
                if (gfg.requestFullscreen) { 
                    gfg.requestFullscreen(); 
                } 
            } 
            function closeFullscreen() { 
                if (document.exitFullscreen) { 
                    document.exitFullscreen(); 
                }  
            } 
            var output = document.getElementById("geeks"); 
            document.addEventListener("fullscreenchange", function() { 
                output.innerHTML = "A computer science portal for geeks"; 
            }); 
        </script> 
    </body> 
      
    </html>                    

    輸出:

    支持的瀏覽器:下麵列出了HTML DOM fullscreenchange事件支持的瀏覽器:
  • 穀歌瀏覽器45.0
  • Internet Explorer 11.0
  • Firefox 47.0
  • 蘋果Safari 5.1
  • Opera 15.0


相關用法


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