gpt4 book ai didi

javascript - 如何使用 document.msFullscreenElement !== null(使用 javascript)检测全屏

转载 作者:行者123 更新时间:2023-11-30 17:22:46 26 4
gpt4 key购买 nike

根据 here 中给出的答案,使用“document.msFullscreenElement !== null” :

我正在尝试检测页面是否在 IE11 中处于全屏模式:

<!DOCTYPE html>
<head>
<title>Detect Fullscreen</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var inFullscreen = document.msFullscreenElement !== null;
alert("inFullscreen is " + inFullscreen);
</script>
</head>
<body>
<p>Detect Fullscreen in IE11</p>
</body>
</html>

但是,无论浏览器是否处于全屏模式,警报的结果都是错误的。大概我误解了这意味着如何应用?

最佳答案

MSFullscreenChange event在元素进入或退出全屏模式后触发,因此您可以获得当前状态。在示例中,我们检查 msFullscreenElement 以查看是否有任何元素处于全屏状态。如果元素处于全屏模式,则返回该元素,否则 msFullscreenElement 返回 null。

if (document.requestFullscreen) {
document.addEventListener("fullscreenchange", function () {
if (document.fullscreenElement != null) {
console.info("Went full screen");
} else {
console.info("Exited full screen");
}
});
}
else if (document.msRequestFullscreen) {
document.addEventListener("MSFullscreenChange", function () {
if (document.msFullscreenElement != null) {
console.info("Went full screen");
} else {
console.info("Exited full screen");
}
});
}

关于javascript - 如何使用 document.msFullscreenElement !== null(使用 javascript)检测全屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24827399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com