1

我想自动使页面全屏而不需要单击任何内容。我试图将其更改 <body onclick="toggleFullScreen()"><body onload="toggleFullScreen()">. 但什么也没发生;它不起作用。:(

这是javascript..

function errorHandler() {
   alert('mozfullscreenerror');
}
document.documentElement.addEventListener('mozfullscreenerror', errorHandler, false);

// toggle full screen

function toggleFullScreen() {
  if (!document.fullscreenElement &&    // alternative standard method
      !document.mozFullScreenElement && !document.webkitFullscreenElement) {  // current working methods
    if (document.documentElement.requestFullscreen) {
      document.documentElement.requestFullscreen();
    } else if (document.documentElement.mozRequestFullScreen) {
      document.documentElement.mozRequestFullScreen();
    } else if (document.documentElement.webkitRequestFullscreen) {
      document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
    }
  } else {
    if (document.cancelFullScreen) {
      document.cancelFullScreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    } else if (document.webkitCancelFullScreen) {
      document.webkitCancelFullScreen();
    }
  }
}

4

1 回答 1

1

You can't execute any RequestFullScreen without a user gesture in any browser.

By the way your full screen function can be like this :

  function fullScreen() {
    var el = document.documentElement;
    var rfs = el.requestFullScreen
              || el.webkitRequestFullScreen
              || el.mozRequestFullScreen;
    rfs.call(el);
  }
于 2015-03-11T07:54:56.603 回答