简体   繁体   English

如何使用HTML5的全屏API检查元素是否全屏显示

[英]How can I check if an element is in fullscreen with the fullscreen API for HTML5

The question is pretty much in the title. 问题几乎在标题中。 I want to return a value that tells me whether or not the element in question is in fullscreen. 我想返回一个值,告诉我有问题的元素是否全屏显示。 How do I do that using javascript? 我怎么用javascript做到这一点? This seems to not work: 这似乎不起作用:

function fs_status()
{
var fullscreenElement = canvas.fullscreenElement ||canvas.mozFullscreenElement || canvas.webkitFullscreenElement;
return fullscreenElement;
}

//Sorry, I don't really understand js. //对不起,我真的不懂js。 So if your making an example can you please use 'canvas' as the element in question. 因此,如果你做一个例子,请使用'canvas'作为问题。

I found out how to do it after playing around a little: 我玩了一下之后发现了怎么做:

function fs_status() {
    if (document.fullscreenElement || document.webkitFullscreenElement ||
        document.mozFullScreenElement)
            return 1;
    else
            return -1;
}

Sadly, Fullscreen API still is a mess when you try to work on different browsers. 遗憾的是,当您尝试在不同的浏览器上工作时,Fullscreen API仍然是一团糟。
Webkit browsers and Firefox do include a isFullscreen property to the document, but not IE. Webkit浏览器和Firefox确实包含文档的isFullscreen属性,但不包括IE。 But, you can use a workaround looking for msFullscreenElement which might be set to undefined if no fullscreen was requested. 但是,您可以使用查找msFullscreenElement的变通方法,如果未请求全屏,则可能将其设置为undefined

Here is kinda polyfill you can use : 这里有一种你可以使用的polyfill:

if (typeof(document.isFullscreen === undefined)) {
  document.isFullscreen = function() {
    return document.webkitIsFullscreen || //Webkit browsers
           document.mozFullScreen || // Firefox
           document.msFullscreenElement !== undefined; // IE
  };
}

Didn't tested it on IE but it should work. 没有在IE上测试它,但它应该工作。

Note : call it like that : if( document.isFullscreen() ){ fullscreenOn }else{ fullscreenOff } 注意:这样称呼: if( document.isFullscreen() ){ fullscreenOn }else{ fullscreenOff }

Here's a one-line if clause that will work cross-browser: 这是一个可以跨浏览器工作的单行if子句:

if (!document.isFullScreen && !document.fullscreenElement && !document.webkitFullscreenElement && !document.mozFullScreenElement && !document.msFullscreenElement) {
     .... do something
}

This checks to see if you're currently in full-screen mode across Chrome, Opera, Firefox, IE11 and Edge. 这会检查您是否目前处于Chrome,Opera,Firefox,IE11和Edge的全屏模式。 All of the HTML5 full screen stuff isn't currently standard cross browser, so we need to check for the existence of various different prefixes in the if clause. 所有HTML5全屏幕内容当前都不是标准的跨浏览器,因此我们需要检查if子句中是否存在各种不同的前缀。 Here's the documentation to show what different prefixes need to be used across different browsers: 这是用于显示不同浏览器需要使用哪些不同前缀的文档:

https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API#Prefixing https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API#Prefixing

Note - to check whether or not you're in full screen, you always need to check the full screen property of the document element (as I've shown in the example) - it's not a property of the element that is full screen. 注意 - 要检查您是否全屏,您始终需要检查文档元素的全屏属性(如我在示例中所示) - 它不是全屏元素的属性。

Kaaido's answer didn't quite work for me in Chrome, but I liked the approach. Kaaido的答案在Chrome中对我不起作用,但我喜欢这种方法。 Updated code comes out to: 更新的代码出现在:

 if (typeof(document.isFullscreen === undefined)) { document.isFullscreen = function() { return !((document.fullScreenElement !== undefined && document.fullScreenElement === null) || (document.msFullscreenElement !== undefined && document.msFullscreenElement === null) || (document.mozFullScreen !== undefined && !document.mozFullScreen) || (document.webkitIsFullScreen !== undefined && !document.webkitIsFullScreen)); } } 

I have just merged Samuel Mungy's answer and Kaiido's answer and add some readibility. 我刚刚合并了Samuel Mungy的答案和Kaiido的答案,并添加了一些可读性。

 if (document.isFullscreen === undefined) {

    var fs = function () {
      if(document.fullscreenElement !== undefined) return document.fullscreenElement;
      if(document.webkitFullscreenElement !== undefined) return document.webkitFullscreenElement;
      if(document.mozFullScreenElement !== undefined) return document.mozFullScreenElement;
      if(document.msFullscreenElement !== undefined) return document.msFullscreenElement;
    }
    if (fs() === undefined) document.isFullscreen = undefined;
    else document.isFullscreen = fs;
  }

Question specific part: I assume that 问题具体部分:我认为

canvas = document.getElementById("myCanvas");

so you can use this snippet 所以你可以使用这个片段

if(document.isFullscreen !== undefined && document.isFullscreen().getAttribute("id") !== null){

  if(document.isFullscreen().getAttribute("id") === "myCanvas"){
    //your canvas is fullscrren now

  }else{
    //your canvas is not fullscreen now

  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM