如何让这个HTML5全屏功能在Firefox中运行?

时间:2013-12-02 10:52:56

标签: javascript jquery html html5 cross-browser

我想要一个使用HTML5全屏API触发全屏模式的按钮。我在网络上关注了一些示例以获取下面的代码,但它仅适用于Safari / Chrome,因为它是特定于webkit的前缀。我想使用moz前缀(以及标准HTML5)也可以在Firefox中使用,但我不知道如何去做。有什么建议吗?

HTML:

<div id="viewer">
     <a href="#" class="fullscreen">Exit/enter fullscreen mode</a>
</div>

JS:

$('.fullscreen').on('click', function(){
     var elem = document.getElementById('viewer');
     if (document.webkitFullscreenElement) {
          document.webkitCancelFullScreen();
     } else {
          elem.webkitRequestFullScreen();
     };
});

1 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

var cancelFullScreen = document.cancelFullScreen 
  || document.webkitCancelFullScreen
  || document.mozCancelFullScreen 
  || document.msCancelFullScreen;

var requestFullScreen = document.requestFullScreen 
  || document.webkitRequestFullScreen
  || document.mozRequestFullScreen 
  || document.msRequestFullScreen;

$('.fullscreen').on('click', function(){
  var elem = document.getElementById('viewer');
  var fullscreenElement = document.fullscreenElement 
    || document.webkitFullscreenElement
    || document.mozFullscreenElement 
    || document.msFullscreenElement;

  if (fullscreenElement) {
    cancelFullScreen.call(document);
  } else {
    requestFullScreen.call(document);
  };
});