js如何检测当前页面是否以全屏模式显示?

您所在的位置:网站首页 js全拼 js如何检测当前页面是否以全屏模式显示?

js如何检测当前页面是否以全屏模式显示?

2024-01-13 13:19| 来源: 网络整理| 查看: 265

"```js

function isFullScreen() { return ( (document.fullscreenElement && document.fullscreenElement !== null) || (document.webkitFullscreenElement && document.webkitFullscreenElement !== null) || (document.mozFullScreenElement && document.mozFullScreenElement !== null) || (document.msFullscreenElement && document.msFullscreenElement !== null) ); }

上述代码是用于检测当前页面是否以全屏模式显示的 JavaScript 函数。它通过检查不同浏览器的全屏属性来确定是否处于全屏模式。

函数内部使用了四个属性来检测全屏状态:

document.fullscreenElement:标准 API,在全屏模式下返回当前全屏元素,否则返回 null。 document.webkitFullscreenElement:Webkit 内核浏览器 API,在全屏模式下返回当前全屏元素,否则返回 null。 document.mozFullScreenElement:Firefox 浏览器 API,在全屏模式下返回当前全屏元素,否则返回 null。 document.msFullscreenElement:IE 浏览器 API,在全屏模式下返回当前全屏元素,否则返回 null。

函数通过逻辑运算符 || 来判断是否有其中一个属性不为 null,即可确定页面是否处于全屏模式。

调用该函数可以返回一个布尔值,true 表示页面以全屏模式显示,false 表示页面不是全屏模式。

以下是使用示例:

if (isFullScreen()) { console.log(\"页面处于全屏模式\"); } else { console.log(\"页面不是全屏模式\"); }

请注意,全屏模式可能会受到浏览器的限制,比如需要用户操作才能进入全屏模式,或者在某些情况下无法退出全屏。因此,在实际使用中,需要根据具体需求和浏览器的支持情况进行测试和处理。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3