触发全屏模式时,在网页中禁用滚动

时间:2017-07-11 10:16:14

标签: javascript css fullscreen

我正在尝试使用javscript获取全屏模式。所以我找到了this回答并正在努力。但我发现,当我使用此代码触发全屏时,滚动条消失并且滚动被禁用。但如果我使用F11触发相同的内容,则会出现滚动条并可以滚动。  我试图给overflow:auto身体,但它确实没有任何效果。 Here是重现同一问题的小提琴。当您运行小提琴时,您将能够看到所有段落都不可见。

注意:我使用的是Firefox 45。

HTML:

<body>
      <input type="button" value="click to go fullscreen" onclick="requestFullScreen()">
      <p>
       1 some text to make screnn bigger
      </p>
        <p>
         2some text to make screnn bigger
      </p>
        <p>
       3 some text to make screnn bigger
      </p>
        <p>
        4some text to make screnn bigger
      </p>
        <p>
        5some text to make screnn bigger
      </p>
        <p>
       6 some text to make screnn bigger
      </p>
        <p>
        7some text to make screnn bigger
      </p>  <p>
       8 some text to make screnn bigger
      </p>
        <p>
       9 some text to make screnn bigger
      </p>
        <p>
       10 some text to make screnn bigger
      </p>
        <p>
       11some text to make screnn bigger
      </p>
        <p>
        12 some text to make screnn bigger
      </p>
        <p>
        13 some text to make screnn bigger
      </p>
        <p>
       14 some text to make screnn bigger
      </p>
        <p>
       15 some text to make screnn bigger
      </p>
        <p>
        16 some text to make screnn bigger
      </p>
    </body>

使用Javascript:

function requestFullScreen() {

  var el = document.body;

  // Supports most browsers and their versions.
  var requestMethod = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen;

  if (requestMethod) {

    // Native full screen.
    requestMethod.call(el);

  } else if (typeof window.ActiveXObject !== "undefined") {

    // Older IE.
    var wscript = new ActiveXObject("WScript.Shell");

    if (wscript !== null) {
      wscript.SendKeys("{F11}");
    }
  }
}

1 个答案:

答案 0 :(得分:2)

el更改为document.documentElement,滚动功能将在Firefox中使用。

var el = document.documentElement;

在使用Chrome时它会在JSFiddle中破解(因为我猜的是iframe政策),但我认为这不是主要用途。