首页 > 解决方案 > msRequestFullscreen 在 keydown 事件(IE 11)的情况下不起作用

问题描述

我正在使用全屏 api。

我已经添加了这个 polyfill:

let doc = document as any;
if (!doc.requestFullscreen) {
  document.body.requestFullscreen = doc.body.mozRequestFullScreen || 
    doc.body.webkitRequestFullscreen || doc.body.msRequestFullscreen;
  document.exitFullscreen = doc.mozCancelFullScreen || doc.webkitExitFullscreen || 
    doc.msExitFullscreen;
}

我有用于启用全屏模式的 UI 按钮,并且一切正常(chrome IE 11、edge、opera、firefox)

我也有 keydown 处理程序:

if (args.keyCode === 70) {
  args.preventDefault();
  if (!this.isInFullScreen) {
    document.body.requestFullscreen();
  } 
  else {
    document.exitFullscreen();
  }
  this.InFullScreen = !this.isInFullScreen;
}

但是通过按 F 启用/禁用全屏模式在 IE 11中不起作用。 msRequestFullscreen 函数根本不执行任何操作。没有控制台错误或smth。在其他浏览器中工作正常。
我该如何解决这个问题?

标签: javascriptinternet-explorerkeydownhtml5-fullscreen

解决方案


我可以重现这个问题。在 IE 11 中工作,但它在事件msRequestFullscreen中不起作用。keyCode

作为一种解决方法,我建议您可以使用ActiveXObjectF11 SendKeys。它可以在 IE 11 中使其全屏显示。您可以添加以下代码:

if ("ActiveXObject" in window) {
   var wscript = new ActiveXObject("Wscript.shell");
   wscript.SendKeys("{F11}");
}

推荐阅读