如何使用Javascript使窗口全屏(在整个屏幕上拉伸)
2022-08-30 00:15:30
						我如何使用JavaScript使访问者的浏览器全屏显示,以一种与IE,Firefox和Opera一起使用的方式?
我如何使用JavaScript使访问者的浏览器全屏显示,以一种与IE,Firefox和Opera一起使用的方式?
在较新的浏览器中,如Chrome 15,Firefox 10,Safari 5.1,IE 10,这是可能的。较旧的IE也可以通过ActiveX进行,具体取决于其浏览器设置。
操作方法如下:
function requestFullScreen(element) {
    // Supports most browsers and their versions.
    var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
    if (requestMethod) { // Native full screen.
        requestMethod.call(element);
    } else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
        var wscript = new ActiveXObject("WScript.Shell");
        if (wscript !== null) {
            wscript.SendKeys("{F11}");
        }
    }
}
var elem = document.body; // Make the body go full screen.
requestFullScreen(elem);
用户显然需要首先接受全屏请求,并且不可能在页面加载时自动触发,它需要由用户触发(例如按钮)
阅读更多: https://developer.mozilla.org/en/DOM/Using_full-screen_mode
此代码还包括如何为 Internet Explorer 9 启用全屏,可能还有旧版本,以及最新版本的 Google Chrome。接受的答案也可以用于其他浏览器。
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}");
  }
}
来源:
requestFullscreen