如何使用HTML5的全屏API检查元素是否在全屏中
How can I check if an element is in fullscreen with the fullscreen API for HTML5
这个问题几乎就在标题中。我想返回一个值,告诉我有问题的元素是否在全屏中。如何使用javascript实现这一点?这似乎不起作用:
function fs_status()
{
var fullscreenElement = canvas.fullscreenElement ||canvas.mozFullscreenElement || canvas.webkitFullscreenElement;
return fullscreenElement;
}
//对不起,我真的不懂js。因此,如果你在做一个例子,请使用"画布"作为有问题的元素。
我在玩了一点之后发现了如何做到这一点:
function fs_status() {
if (document.fullscreenElement || document.webkitFullscreenElement ||
document.mozFullScreenElement)
return 1;
else
return -1;
}
遗憾的是,当您尝试在不同的浏览器上工作时,全屏API仍然是一团糟
Webkit浏览器和Firefox确实在文档中包含isFullscreen
属性,但不包含IE。但是,您可以使用一种变通方法来查找msFullscreenElement
,如果没有请求全屏,则可能会将其设置为undefined
。
这里有一种你可以使用的polyfill:
if (typeof(document.isFullscreen === undefined)) {
document.isFullscreen = function() {
return document.webkitIsFullscreen || //Webkit browsers
document.mozFullScreen || // Firefox
document.msFullscreenElement !== undefined; // IE
};
}
没有在IE上测试过,但它应该可以工作。
注意:这样称呼它:if( document.isFullscreen() ){ fullscreenOn }else{ fullscreenOff }
这里有一个单行if子句,可以跨浏览器工作:
if (!document.isFullScreen && !document.fullscreenElement && !document.webkitFullscreenElement && !document.mozFullScreenElement && !document.msFullscreenElement) {
.... do something
}
这会检查您当前是否处于Chrome、Opera、Firefox、IE11和Edge的全屏模式。所有HTML5全屏内容目前都不是标准的跨浏览器,所以我们需要检查if子句中是否存在各种不同的前缀。以下文档显示了在不同的浏览器中需要使用哪些不同的前缀:
https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API#Prefixing
注意-要检查您是否处于全屏状态,您总是需要检查文档元素的全屏属性(如我在示例中所示)-是而不是元素的一个全屏属性。
Kaaido的回答在Chrome中对我来说不太管用,但我喜欢这种方法。更新后的代码发布到:
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));
}
}
我刚刚合并了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;
}
问题特定部分:我假设
canvas = document.getElementById("myCanvas");
所以你可以使用这个片段
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
}
}
- javascript数组元素是否知道其封闭数组
- 如何检查元素是否在iframe中
- 检查元素是否将状态从隐藏更改为可见
- 确定元素是否存在
- 消隐数组元素是否生成自己的属性
- 检查一个元素是否有一个类与另一个类总是返回true
- 如何检查日期选择器和时间选择器元素是否使用JQuery/Javascript设置
- 如何查找具有该ID的元素是否存在
- 检查元素是否已单击或hasClass
- 使用webdriver和selenium验证元素是否不存在
- 如何使用jQuery实时检查元素是否为空
- html元素是否具有内置的属性
- 检查元素是否有内容
- 使用 javascript 检查元素是否包含子标记而不是文本
- JavaScript 在 JSON 对象中移动如何判断元素是否存在
- 检查两个或多个 DOM 元素是否重叠
- 谷歌地球插件 — 如何检查带有 ID 的元素是否已经存在
- 检查元素是否悬停在上面
- DOM 元素是否可以具有其值为任意对象(而不是字符串)的属性
- 如何检查DOM元素是否已完全加载