简体   繁体   English

检测浏览器是否处于全屏模式

[英]Detecting if a browser is in full screen mode

Is there any way of reliably detecting if a browser is running in full screen mode?有什么方法可以可靠地检测浏览器是否以全屏模式运行? I'm pretty sure there isn't any browser API I can query, but has anyone worked it out by inspecting and comparing certain height/width measurements exposed by the DOM?我很确定没有任何浏览器 API 我可以查询,但是有没有人通过检查和比较 DOM 公开的某些高度/宽度测量来解决这个问题? Even if it only works for certain browsers I'm interested in hearing about it.即使它仅适用于某些浏览器,我也有兴趣了解它。

Chrome 15, Firefox 10, and Safari 5.1 now provide APIs to programmatically trigger fullscreen mode. Chrome 15,Firefox 10和Safari 5.1现在提供API,以编程方式触发全屏模式。 Fullscreen mode triggered this way provides events to detect fullscreen changes and CSS pseudo-classes for styling fullscreen elements. 以这种方式触发的全屏模式提供事件以检测全屏更改,并提供CSS伪类以设置全屏元素的样式。

See this hacks.mozilla.org blog post for details. 有关详细信息,请参见hacks.mozilla.org博客

Opera treats full screen as a different CSS media type. Opera将全屏视为另一种CSS媒体类型。 They call it Opera Show , and you can control it yourself easily: 他们将其称为Opera Show ,您可以自己轻松控制它:

@media projection {
  /* these rules only apply in full screen mode */
}

Combined with Opera@USB , I've personally found it extremely handy. 结合Opera @ USB ,我个人觉得非常方便。

What about determining the distance between the viewport width and the resolution width and likewise for height. 确定视口宽度与分辨率宽度之间的距离以及高度的关系又如何呢? If it is a small amount of pixels (especially for height) it may be at fullscreen. 如果像素少(尤其是高度),则可能处于全屏状态。

However, this will never be reliable. 但是,这将永远是不可靠的。

Just thought I'd add my thruppence to save anyone banging their heads. 只是以为我会加油打气,以免任何人摔打头。 The first answer is excellent if you have complete control over the process, that is you initiate the fullscreen process in code. 如果您完全控制该过程,即用代码启动全屏过程,则第一个答案是极好的。 Useless should anyone do it thissen by hitting F11. 任何人都无法通过按F11来这样做。

The glimmer of hope on the horizon come in the form of this W3C recommendation http://www.w3.org/TR/view-mode/ which will enable detection of windowed, floating (without chrome), maximized, minimized and fullscreen via media queries (which of course means window.matchMedia and associated). W3C建议http://www.w3.org/TR/view-mode/的形式带来了希望的曙光,它可以通过以下方式检测开窗,浮动(无镀铬),最大化,最小化和全屏显示媒体查询(这当然意味着window.matchMedia和相关联)。

I've seen signs that it's in the implementation process with -webkit and -moz prefixes but it doesn't appear to be in production yet. 我已经看到有迹象表明它在实现过程中带有-webkit和-moz前缀,但似乎还没有在生产中。

So no, no solutions but hopefully I'll save someone doing a lot of running around before hitting the same wall. 因此,没有,没有解决方案,但希望我能避免有人在碰壁之前做很多事情。

PS *:-moz-full-screen does doo-dah as well, but nice to know about. PS *:-moz-full-screen也可以播放doo-dah,但很高兴知道。

Firefox 3+在window对象上提供了一个非标准属性,该属性报告浏览器是否处于全屏模式: window.fullScreen

You can check if document.fullscreenElement is not null to determine if fullscreen mode is on. 您可以检查document.fullscreenElement是否不为null以确定全屏模式是否打开。 You'll need to vendor prefix fullscreenElement accordingly. 您需要相应地供应商前缀fullscreenElement I would use something like this: 我会用这样的东西:

var fullscreenElement = document.fullscreenElement || document.mozFullScreenElement ||
document.webkitFullscreenElement || document.msFullscreenElement;

https://msdn.microsoft.com/en-us/library/dn312066(v=vs.85).aspx has a good example for this which I quote below: https://msdn.microsoft.com/zh-cn/library/dn312066(v=vs.85).aspx对此有一个很好的示例,在下面引用:

document.addEventListener("fullscreenChange", function () {
          if (fullscreenElement != null) {
              console.info("Went full screen");
          } else {
              console.info("Exited full screen");              
          }
      });

In Chrome at least: 至少在Chrome中:

onkeydown can be used to detect the F11 key being pressed to enter fullscreen. onkeydown可用于检测按下F11键进入全屏状态。 onkeyup can be used to detect the F11 key being pressed to exit fullscreen. onkeyup可用于检测按F11键退出全屏状态。

Use that in conjunction with checking for keyCode == 122 与检查keyCode == 122结合使用

The tricky part would be to tell the keydown/keyup not to execute its code if the other one just did. 棘手的部分是告诉keydown / keyup如果另一个刚刚执行了,则不要执行其代码。

Right. 对。 Totally late on this one... 完全迟到了这个...

As of 25th Nov, 2014 (Time of writing), it is possible for elements to request fullscreen access, and subsequently control entering/exiting fullscreen mode. 自2014年11月25日(撰写本文时)起,元素可以请求全屏访问,并随后控制进入/退出全屏模式。

MDN Explanation here: https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode MDN说明: https//developer.mozilla.org/zh-CN/docs/Web/Guide/API/DOM/Using_full_screen_mode

Straightforward explanation by David Walsh: http://davidwalsh.name/fullscreen David Walsh的简单解释: http : //davidwalsh.name/fullscreen

The Document read-only property returns the Element that is currently being presented in full-screen mode in this document, or null if full-screen mode is not currently in use. Document只读属性返回当前在此文档中以全屏模式显示的Element,如果当前未使用全屏模式,则返回null。

if(document.fullscreenElement){
  console.log("Fullscreen");
}else{
  console.log("Not Fullscreen");
};

Supports in all major browsers. 在所有主要浏览器中都支持。

There is my NOT cross-browser variant: 有我的NOT跨浏览器变体:

<!DOCTYPE html>
<html>
<head>
  <title>Fullscreen</title>
</head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
var fullscreen = $(window).height() + 1 >= screen.height;
$(window).on('resize', function() {
  if (!fullscreen) {
    setTimeout(function(heightStamp) {
      if (!fullscreen && $(window).height() === heightStamp && heightStamp + 1 >= screen.height) {
        fullscreen = true;
        $('body').prepend( "<div>" + $( window ).height() + " | " + screen.height + " | fullscreen ON</div>" );
      }
    }, 500, $(window).height());
  } else {
    setTimeout(function(heightStamp) {
      if (fullscreen && $(window).height() === heightStamp && heightStamp + 1 < screen.height) {
        fullscreen = false;
        $('body').prepend( "<div>" + $( window ).height() + " | " + screen.height + " | fullscreen OFF</div>" );
      }
    }, 500, $(window).height());
  }
});
</script>
</body>
</html>

Tested on: 经过测试:
Kubuntu 13.10 : Kubuntu 13.10
Firefox 27 ( <!DOCTYPE html> is required, script correctly works with dual-monitors), Chrome 33, Rekonq - pass Firefox 27( <!DOCTYPE html>是必需的,脚本可在双显示器上正常工作),Chrome 33,Rekonq-通过

Win 7 : 赢7
Firefox 27, Chrome 33, Opera 12, Opera 20, IE 10 - pass Firefox 27,Chrome 33,Opera 12,Opera 20,IE 10-通过
IE < 10 - fail IE <10-失败

My solution is: 我的解决方案是:

var fullscreenCount = 0;
var changeHandler = function() {                                           

    fullscreenCount ++;

    if(fullscreenCount % 2 === 0)
    {
        console.log('fullscreen exit');
    }
    else
    {
        console.log('fullscreened');
    }

}                                                                         
document.addEventListener("fullscreenchange", changeHandler, false);      
document.addEventListener("webkitfullscreenchange", changeHandler, false);
document.addEventListener("mozfullscreenchange", changeHandler, false);
document.addEventListener("MSFullscreenChanges", changeHandler, false);

This is the solution that I've come to... I wrote it as an es6 module but the code should be pretty straightforward. 这就是我要解决的解决方案...我将其编写为es6模块,但代码应该非常简单。

/**
 * Created by sam on 9/9/16.
 */
import $ from "jquery"

function isFullScreenWebkit(){
    return $("*:-webkit-full-screen").length > 0;
}
function isFullScreenMozilla(){
    return $("*:-moz-full-screen").length > 0;
}
function isFullScreenMicrosoft(){
    return $("*:-ms-fullscreen").length > 0;
}

function isFullScreen(){
    // Fastist way
    var result =
        document.fullscreenElement ||
        document.mozFullScreenElement ||
        document.webkitFullscreenElement ||
        document.msFullscreenElement;

    if(result) return true;

    // A fallback
    try{
        return isFullScreenMicrosoft();
    }catch(ex){}
    try{
        return isFullScreenMozilla();
    }catch(ex){}
    try{
        return isFullScreenWebkit();
    }catch(ex){}

    console.log("This browser is not supported, sorry!");
    return false;
}

window.isFullScreen = isFullScreen;

export default isFullScreen;

2021, theFullscreen API is available. 2021年,全屏API上市。 It's a Living Standard and is supported by all browsers (except the usual suspects - IE11 and iOS Safari).这是一个生活标准,所有浏览器都支持(除了通常的嫌疑人 - IE11 和 iOS Safari)。

// toggle fullscreen

      if (!document.fullscreenElement) {
        // enter fullscreen
        if (docElm.requestFullscreen) {
          console.log('entering fullscreen')
          docElm.requestFullscreen()
        }
      } else {
        // exit fullscreen
        if (document.exitFullscreen) {
          console.log('exiting fullscreen')
          document.exitFullscreen()
        }
      }

This works for all new browsers : 这适用于所有新浏览器:

if (!window.screenTop && !window.screenY) { 
   alert('Browser is in fullscreen');
}

User window.innerHeight and screen.availHeight . 用户window.innerHeightscreen.availHeight Also the widths. 还有宽度。

window.onresize = function(event) {
    if (window.outerWidth === screen.availWidth && window.outerHeight === screen.availHeight) {
        console.log("This is your MOMENT of fullscreen: " + Date());    
}

While searching high & low I have found only half-solutions. 在搜索高低时,我只找到一半的解决方案。 So it's better to post here a modern, working approach to this issue: 因此,最好在此发布一种现代且可行的方法来解决此问题:

var isAtMaxWidth = (screen.availWidth - window.innerWidth) === 0;
var isAtMaxHeight = (screen.availHeight - window.outerHeight <= 1);
if (!isAtMaxWidth || !isAtMaxHeight) {
       alert("Browser NOT maximized!");
}

Tested and working properly in Chrome, Firefox, Edge and Opera* (*with Sidebar unpinned) as of 10.11.2019. 截至2019年11月10日,已在Chrome,Firefox,Edge和Opera *(*禁用补充工具栏)中进行测试并正常工作。 Testing environment (only desktop): 测试环境(仅台式机):

CHROME - Ver. 78.0.3904.97 (64-bit)
FIREFOX - Ver. 70.0.1 (64-bit)
EDGE - Ver. 44.18362.449.0 (64-bit)
OPERA - Ver. 64.0.3417.92 (64-bit)
OS - WIN10 build 18362.449 (64-bit)

Resources: 资源:

To detect whether browser is in fullscreen mode:检测浏览器是否处于全屏模式:

document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement

according to caniuse you should be fine for majority of browsers.根据caniuse的说法,您应该适合大多数浏览器。

This property returns the Element that is currently in fullscreen mode.此属性返回当前处于全屏模式的元素。

document.fullscreenElement; // HTML Element or null

Also, you can subscribe to fullscreen change events with this method此外,您可以使用此方法订阅全屏更改事件

addEventListener('fullscreenchange', (event) => { });

You can combine both to detect the nature of the change您可以将两者结合起来以检测更改的性质

addEventListener('fullscreenchange', () => { 
 if (document.fullscreenElement) {
   // Your Logic if fullscreen
 }
});

More on thishere .更多关于这个here

You can detect full screen using CSS like this:您可以像这样使用 CSS 检测全屏:

@media all and (display-mode: fullscreen) {
  // Regular CSS to be applied in full-screen mode
}

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

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