How to make the window full screen with Javascript (stretching all over the screen)

后端 未结 19 1794
天涯浪人
天涯浪人 2020-11-21 22:13

How can I make a visitor\'s browser go fullscreen using JavaScript, in a way that works with IE, Firefox and Opera?

相关标签:
19条回答
  • 2020-11-21 22:41

    This will works to show your window in full screen

    Note: For this to work, you need Query from http://code.jquery.com/jquery-2.1.1.min.js

    Or make have javascript link like this.

    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    
       <div id="demo-element">
            <span>Full Screen Mode Disabled</span>
            <button id="go-button">Enable Full Screen</button>
        </div>
        <script>
        function GoInFullscreen(element) {
            if(element.requestFullscreen)
                element.requestFullscreen();
            else if(element.mozRequestFullScreen)
                element.mozRequestFullScreen();
            else if(element.webkitRequestFullscreen)
                element.webkitRequestFullscreen();
            else if(element.msRequestFullscreen)
                element.msRequestFullscreen();
        }
    
        function GoOutFullscreen() {
            if(document.exitFullscreen)
                document.exitFullscreen();
            else if(document.mozCancelFullScreen)
                document.mozCancelFullScreen();
            else if(document.webkitExitFullscreen)
                document.webkitExitFullscreen();
            else if(document.msExitFullscreen)
                document.msExitFullscreen();
        }
    
        function IsFullScreenCurrently() {
            var full_screen_element = document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement || null;
    
            if(full_screen_element === null)
                return false;
            else
                return true;
        }
    
        $("#go-button").on('click', function() {
            if(IsFullScreenCurrently())
                GoOutFullscreen();
            else
                GoInFullscreen($("#demo-element").get(0));
        });
    
        $(document).on('fullscreenchange webkitfullscreenchange mozfullscreenchange MSFullscreenChange', function() {
            if(IsFullScreenCurrently()) {
                $("#demo-element span").text('Full Screen Mode Enabled');
                $("#go-button").text('Disable Full Screen');
            }
            else {
                $("#demo-element span").text('Full Screen Mode Disabled');
                $("#go-button").text('Enable Full Screen');
            }
        });</script>
    
    0 讨论(0)
  • 2020-11-21 22:43

    This code also includes how to enable full screen for Internet Explorer 9, and probably older versions, as well as very recent versions of Google Chrome. The accepted answer may also be used for other browsers.

    var el = document.documentElement
        , rfs = // for newer Webkit and Firefox
               el.requestFullscreen
            || el.webkitRequestFullScreen
            || el.mozRequestFullScreen
            || el.msRequestFullscreen
    ;
    if(typeof rfs!="undefined" && rfs){
      rfs.call(el);
    } else if(typeof window.ActiveXObject!="undefined"){
      // for Internet Explorer
      var wscript = new ActiveXObject("WScript.Shell");
      if (wscript!=null) {
         wscript.SendKeys("{F11}");
      }
    }
    

    Sources:

    • Chrome Fullscreen API (note, however, that requestFullscreen "only works during" "[m]ost UIEvents and MouseEvents, such as click and keydown, etc.", "so it cannot be used maliciously".)
    • How to make browser full screen using F11 key event through JavaScript
    0 讨论(0)
  • 2020-11-21 22:44

    Try screenfull.js. It's a nice cross-browser solution that should work for Opera browser as well.

    Simple wrapper for cross-browser usage of the JavaScript Fullscreen API, which lets you bring the page or any element into fullscreen. Smoothens out the browser implementation differences, so you don't have to.

    Demo.

    0 讨论(0)
  • 2020-11-21 22:45

    Now that the full screen APIs are more widespread and appear to be maturing, why not try Screenfull.js? I used it for the first time yesterday and today our app goes truly full screen in (almost) all browsers!

    Be sure to couple it with the :fullscreen pseudo-class in CSS. See https://www.sitepoint.com/use-html5-full-screen-api/ for more.

    0 讨论(0)
  • 2020-11-21 22:50

    Simple example from: http://www.longtailvideo.com/blog/26517/using-the-browsers-new-html5-fullscreen-capabilities/

    <script type="text/javascript">
      function goFullscreen(id) {
        // Get the element that we want to take into fullscreen mode
        var element = document.getElementById(id);
    
        // These function will not exist in the browsers that don't support fullscreen mode yet, 
        // so we'll have to check to see if they're available before calling them.
    
        if (element.mozRequestFullScreen) {
          // This is how to go into fullscren mode in Firefox
          // Note the "moz" prefix, which is short for Mozilla.
          element.mozRequestFullScreen();
        } else if (element.webkitRequestFullScreen) {
          // This is how to go into fullscreen mode in Chrome and Safari
          // Both of those browsers are based on the Webkit project, hence the same prefix.
          element.webkitRequestFullScreen();
       }
       // Hooray, now we're in fullscreen mode!
      }
    </script>
    
    <img class="video_player" src="image.jpg" id="player"></img>
    <button onclick="goFullscreen('player'); return false">Click Me To Go Fullscreen! (For real)</button>
    
    0 讨论(0)
  • 2020-11-21 22:51

    A Q&D way to go full screen, if you are in a "kiosk" situation, is to feed an F11 to the browser window after it's up and running. This is not pretty starting up and the user might be able to poke a touch screen at the top and get a semi-full-screen view, but feeding the F11 might do in a pinch or just to get started on a project.

    0 讨论(0)
提交回复
热议问题