简体   繁体   English

检测全屏模式

[英]Detect fullscreen mode

Modern desktop version of IE 10 is always fullscreen. IE 10 的现代桌面版始终是全屏的。

There is a living specification for :fullscreen pseudo-class on W3 W3 上有:fullscreen伪类的动态规范

But when I tried to detect fullscreen with jQuery version 1.9.x and 2.x:但是当我尝试使用 jQuery 版本 1.9.x 和 2.x 检测全屏时:

$(document).is(":fullscreen") 

it threw this error:它抛出了这个错误:

Syntax error, unrecognized expression: fullscreen语法错误,无法识别的表达式:全屏

Questions:问题:

  1. Is it because jQuery doesn't recognize this standard yet or IE10?是jQuery还不识别这个标准还是IE10?

  2. What is the legacy way of checking fullscreen mode?检查全屏模式的传统方法是什么? I am expecting following results:我期待以下结果:

     function IsFullScreen() { /* Retrun TRUE */ /* If UA exists in classic desktop and not in full screen */ /* Else return FALSE */ }
  3. Can we do it without browser sniffing?我们可以不用浏览器嗅探吗?

As you have discovered, browser compatibility is a big drawback.正如您所发现的,浏览器兼容性是一个很大的缺点。 After all, this is something very new.毕竟,这是非常新的东西。

However, since you're working in JavaScript, you have far more options available to you than if you were just using CSS.但是,由于您使用的是 JavaScript,因此与仅使用 CSS 相比,您有更多的选择。

For example:例如:

if( window.innerHeight == screen.height) {
    // browser is fullscreen
}

You can also check for some slightly more loose comparisons:您还可以检查一些稍微宽松的比较:

if( (screen.availHeight || screen.height-30) <= window.innerHeight) {
    // browser is almost certainly fullscreen
}

an event is fired when the browser changes full screen mode.当浏览器改变全屏模式时会触发一个事件。 You can use that to set a variable value, which you can check to determine if the browser is full screen or not.您可以使用它来设置变量值,您可以检查该值以确定浏览器是否全屏。

this.fullScreenMode = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen; // This will return true or false depending on if it's full screen or not.

$(document).on ('mozfullscreenchange webkitfullscreenchange fullscreenchange',function(){
       this.fullScreenMode = !this.fullScreenMode;

      //-Check for full screen mode and do something..
      simulateFullScreen();
 });





var simulateFullScreen = function() {
     if(this.fullScreenMode) {
            docElm = document.documentElement
            if (docElm.requestFullscreen) 
                docElm.requestFullscreen()
            else{
                if (docElm.mozRequestFullScreen) 
                   docElm.mozRequestFullScreen()
                else{
                   if (docElm.webkitRequestFullScreen)
                     docElm.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT)
                }
            }
     }else{
             if (document.exitFullscreen) 
                  document.exitFullscreen()
             else{ 
                  if (document.mozCancelFullScreen) 
                     document.mozCancelFullScreen()
                  else{
                     if (document.webkitCancelFullScreen) 
                         document.webkitCancelFullScreen();
                  }
             }
     }

     this.fullScreenMode= !this.fullScreenMode

}

This will work on IE9+ and other modern browsers这将适用于 IE9+ 和其他现代浏览器

function isFullscreen(){ return 1 >= outerHeight - innerHeight };

Using "1" (instead of zero) because the system, at times, might just reserve a one pixel height for mouse interaction with some hidden or sliding command bars, in which case the fullscreen detection would fail.使用“1”(而不是零)是因为系统有时可能只为鼠标与某些隐藏或滑动命令栏的交互保留一个像素的高度,在这种情况下全屏检测将失败。

  • will also work for any separate document-element going on the full screen mode at any time.适用于任何时候在全屏模式下运行的任何单独的文档元素

Use fullscreenchange event to detect a fullscreen change event, or if you don't want to handle vendor prefixes than you can also listen to the resize event (the window resize event that also triggers when fullscreen is entered or exited) and then check if document.fullscreenElement is not null to determine if fullscreen mode is on.使用fullscreenchange事件来检测全屏更改事件,或者如果您不想处理供应商前缀,则还可以侦听resize事件(进入或退出全屏时也会触发的窗口调整大小事件),然后检查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/en-us/library/dn312066(v=vs.85).aspx有一个很好的例子,我在下面引用:

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

Reading MDN Web docs, I like this native method.阅读 MDN Web 文档,我喜欢这种原生方法。

https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/fullscreenElement https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/fullscreenElement

function is_fullscreen(){
    return document.fullscreenElement != null;
}

or fancier或发烧友

let is_fullscreen = () => !! document.fullscreenElement

This method also works when you open developer tools in browser.当您在浏览器中打开开发人员工具时,此方法也适用。 Because fullscreen is applied to a particular element, null means none of it is in fullscreen.因为 fullscreen 应用于特定元素,所以 null 意味着它没有全屏显示。

You can even extend to check a particular element eg:您甚至可以扩展以检查特定元素,例如:

function is_fullscreen(element=null){
    return element != null? document.fullscreenElement == element:document.fullscreenElement != null;
}

Which only return true if currently is fullscreen and (element is null or element is the fullscreened element)如果当前是全屏并且(元素为空或元素是全屏元素),则仅返回true

Here is the most up to date answer.这是最新的答案。 fully browser compatible with all the prefixes:与所有前缀完全兼容的浏览器:

function IsFullScreen() {
     return !!(document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement)
}

credit to https://developers.google.com/web/fundamentals/native-hardware/fullscreen/归功于https://developers.google.com/web/fundamentals/native-hardware/fullscreen/

DEMO演示

 function IsFullScreen() { console.log(!!(document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement)) }
 <button onclick="IsFullScreen()">log fullscreen state</button>

It is working in IE 8 and I am writing a specific web page for IE 8. I do not need to check if the other browsers support this or not.它在 IE 8 中工作,我正在为 IE 8 编写一个特定的网页。我不需要检查其他浏览器是否支持这个。

function isFullScreen(){
    return window.screenTop == 0 ? true : false;
}

Here is another solution which works in IE 11:这是另一个适用于 IE 11 的解决方案:

$(document).bind('webkitfullscreenchange mozfullscreenchange fullscreenchange MSFullscreenChange', function() {
var isFullScreen = document.fullScreen ||
    document.mozFullScreen ||
    document.webkitIsFullScreen || (document.msFullscreenElement != null);
if (isFullScreen) {
    console.log('fullScreen!');
} else {
    console.log('no fullScreen!');
}

}); });

Did you try $(window) instead of $(document).您是否尝试过 $(window) 而不是 $(document)。 Follow one example http://webification.com/tag/detect-fullscreen-jquery .按照一个示例http://webification.com/tag/detect-fullscreen-jquery

Try this!尝试这个! Works for recent browsers.适用于最近的浏览器。

if (!window.screenTop && !window.screenY) {
    alert('Fullscreen mode......');
}

You can also use this jquery plugin for same.您也可以使用jquery 插件。

$(window).bind("fullscreen-on", function(e) {
alert("FULLSCREEN MODE");
});

Modernizr FTW ? Modernizr FTW Give this gist a try. 试试这个要点吧。

Here's another solution that might work for you:这是另一个可能适合您的解决方案:

function isFullScreen() {
return Math.abs(screen.width - window.innerWidth) < 10; 
}

I prefer to use width since it will help work around tabs and developer info at the bottom.我更喜欢使用宽度,因为它有助于解决底部的选项卡和开发人员信息。

I worked out a good way of doing this:我想出了一个很好的方法来做到这一点:

w = $('body').width();

if (w == '4800' || w == '4320' || w == '4096' || w == '3200' || w == '3072' || w == '2880' || w == '2560' || w == '2400' || w == '2160' || w == '2048' || w == '1920' || w == '1800' || w == '1620' || w == '1600' || w == '1536' || w == '1440' || w == '1280' || w == '1200' || w == '1152' || w == '1080' || w == '1050' || w == '1024' || w == '960' || w == '900' || w == '864' || w == '800' || w == '768' || w == '720') {
      //User is fullscreen
}

Then set the body default width to:然后将正文默认宽度设置为:

body { width: calc(100% - 1px) }

In Safari on iPhone, webkitDisplayingFullscreen property will return true if <video> is playing in fullscreen.在 iPhone 上的 Safari 中,如果<video>正在全屏播放,则webkitDisplayingFullscreen属性将返回true Ref: https://developer.apple.com/documentation/webkitjs/htmlvideoelement/1630493-webkitdisplayingfullscreen参考: https : //developer.apple.com/documentation/webkitjs/htmlvideoelement/1630493-webkitdisplayingfullscreen

Proposed solution using:建议的解决方案使用:

return window.innerHeight == screen.height && window.innerWidth == screen.width;

works fine, but only in case you're not zooming your browser.工作正常,但前提是您没有缩放浏览器。

To handle cases with zoomed screen use following:要处理缩放屏幕的情况,请使用以下内容:

let zoom = window.outerWidth / window.innerWidth;
return (window.innerHeight * zoom) == screen.height && (window.innerWidth * zoom) == screen.width;

to detemine zoom and then multiply window.innerHeight and window.innerWidth .确定缩放,然后乘以window.innerHeightwindow.innerWidth

As CSS is reliable in detecting the fullscreen, you can use something like this:由于 CSS 在检测全屏方面是可靠的,因此您可以使用以下内容:

#detector {
    position: fixed;
    visibily: hidden;
    top: 0;
    left: 0;
}

@media all and (display-mode: fullscreen) {
    #detector {
        top: 1px;
    }
}

Then you set an interval in javascript to check the element's position.然后在 javascript 中设置一个间隔来检查元素的位置。 document.getElementById('detector').getBoundingClientRect().top > 0

You can maintain the fullscreen status in a variable or state if you are on react.如果您正在响应,您可以在变量或状态中保持全屏状态。

You can subscribe to the keydown event on window object.您可以订阅 window 对象上的 keydown 事件。 If the user presses F11 or Ctrl + command + F (macOS), call event.preventDefault() to prevent the browser fullscreen action.如果用户按下F11Ctrl + command + F (macOS),则调用event.preventDefault()以阻止浏览器全屏操作。 Instead you can call the requestFullscreen method or exitFullscreen method to change the fullscreen mode.相反,您可以调用requestFullscreen方法或exitFullscreen方法来更改全屏模式。 In this way, the document.fullscreenElement will always has a value if the browser is in fullscreen mode.这样,如果浏览器处于全屏模式,则document.fullscreenElement将始终具有一个值。

This workaround breaks if the user uses the browser menu to enter fullscreen mode.如果用户使用浏览器菜单进入全屏模式,则此解决方法会中断。

To update a JS variable:更新 JS 变量:

window.matchMedia('(display-mode: fullscreen)').addListener(({ matches }) => {
        if (matches) {
            window.isFullScreen=true;
        } else {
            window.isFullScreen=false;
        }
    });

Thought I'd share this React soln as well, derived from the answers above我想我也会分享这个 React soln,它来自上面的答案

export const isFullScreen = () => {
    const fullScreenElement = document.fullscreenElement
        || document.webkitFullscreenElement
        || document.mozFullScreenElement
        || document.msFullscreenElement
        || null;
    
    if (fullScreenElement === null) return false;
    return true;
};

In a PWA, if you set in the manifest "fullscreen" then you can even do this:在 PWA 中,如果您在清单中设置“全屏”,那么您甚至可以这样做:

window.isInstalled=window.matchMedia('(display-mode: standalone)').matches;
window.isOnHomepage=window.matchMedia('(display-mode: fulscreen)').matches;

Even if in the manifest there is "fullscreen", if the user instead of installing it, adds it as a bookmark to the hompage of the phone, it will register as "standalone", if instead it's installed as a PWA (apk) it will register as "fullscreen".即使在清单中有“全屏”,如果用户没有安装它,而是将它作为书签添加到手机的首页,它将注册为“独立”,如果相反,它作为 PWA (apk) 安装将注册为“全屏”。

var isFullScreen = function()
{
    var dom = document.createElement("img");
    if ("requestFullscreen" in dom
        || "requestFullScreen" in dom
        || "webkitRequestFullScreen" in dom
        || "mozRequestFullScreen" in dom){
        return !0;
    }
    return !1;
}

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

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