onfullscreenchange DOM 事件

发布于 2024-12-19 14:07:52 字数 141 浏览 5 评论 0原文

正如标题所示,我想知道当浏览器进入/离开/退出全屏模式时触发事件的最可靠方法是什么。

注意:我不是问如何全屏显示当前页面,我只是想知道是否有办法对某些任务进行排队,例如,如果用户按 F11 或任何其他相关全屏输入键。

as the title reads, I'd like to know what is the most reliable way to trigger an event when the Browser(s) enters/leaves in/out the fullscreen mode.

note : I'm not asking how to fullscreen the current page, I just want to know whether or not there is a way to queue some tasks if the user, for example, press F11 or any other related fullscreen-entering keys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

月亮是我掰弯的 2024-12-26 14:07:52

screen.widthscreen.height 告诉您用户的屏幕分辨率,因此请尝试以下操作:

var fullscreen;
function onfullscreenchange(full) {
    ...
}

// You might want to use addEventListener and its equivalents instead
window.onresize = function () {
    if (window.outerWidth === screen.width && window.outerHeight === screen.height) {
        if (!fullscreen) {
            fullscreen = true;
            onfullscreenchange(true);
        }
    } else {
        if (fullscreen) {
            fullscreen = false;
            onfullscreenchange(false);
    }
};

我知道这不是执行所有这些操作的最干净或最可靠的方法,但希望它能给你一个想法。值得注意的是,IE<9 需要不同的方法来确定窗口大小,因此我将让您自行查找。

screen.width and screen.height tell you the user's screen resolution, so try this:

var fullscreen;
function onfullscreenchange(full) {
    ...
}

// You might want to use addEventListener and its equivalents instead
window.onresize = function () {
    if (window.outerWidth === screen.width && window.outerHeight === screen.height) {
        if (!fullscreen) {
            fullscreen = true;
            onfullscreenchange(true);
        }
    } else {
        if (fullscreen) {
            fullscreen = false;
            onfullscreenchange(false);
    }
};

I'm aware this isn't the cleanest or most robust way of doing all this, but hopefully it gives you an idea. Notably, IE<9 needs a different approach for determining the window size, so I'll leave you to look that up.

又爬满兰若 2024-12-26 14:07:52

当我偶然发现这个问题时,我正在处理这个活动,我想分享我学到的东西,即使它不能解决这个问题。现代桌面浏览器和 Chrome 现在通过前缀 支持 onfullscreenchange 事件Android,但有一些事情需要记住:

  • 当窗口全屏显示时,此事件不会触发,我知道这听起来很奇怪,但它似乎仅适用于文档及其元素。因此,如果文档的某个元素全屏显示,该事件将触发,但是当使用键盘快捷键使浏览器全屏显示时,它不会触发。

  • ChromeSafari 中,函数可以通过定义文档方法 document.onwebkitfullscreenchange = myFunc; 或定义来订阅此事件元素方法 myElem.onwebkitfullscreenchange = myFunc;,也可以使用 addEventListener myElem.addEventListener("webkitfullscreenchange", myFunc);。在 IEFirefox 中,该事件仅在文档中定义了该方法时才会起作用,并且使用 addEventListener 不会触发该事件

这是此活动的 Codepen 演示,更多信息请参见 MDN 使用全屏模式


更新。来自 MDN 网络文档:

目前,并非所有浏览器都实现了无前缀版本的 API(对于与供应商无关的全屏 API 访问,您可以使用 Fscreen)。

I was working with this event when I stumble with this question, I want to share what I learn about it even though it won't solve this question. The onfullscreenchange event is now supported with prefixes by modern desktop browsers and Chrome for Android, but there are some things to have in mind:

  • This event won't trigger when the window goes fullscreen, I know it sounds weird, but it seems to be intended only for the document and its elements. So if an element of a document goes fullscreen the event will trigger, but when a keyboard shortcut is used to make your browser fullscreen it won't.

  • In Chrome and Safari a function can subscribe to this event either by defining the document method document.onwebkitfullscreenchange = myFunc; or by defining the element method myElem.onwebkitfullscreenchange = myFunc;, also you can use addEventListener myElem.addEventListener("webkitfullscreenchange", myFunc);. In IE and Firefox the event will work only if the method is defined in the document and using addEventListener won't trigger the event.

Here's a Codepen Demo of this event, more info in MDN Using fullscreen mode.


Update. From MDN web docs:

For the moment not all browsers are implementing the unprefixed version of the API (for vendor agnostic access to the Fullscreen API you can use Fscreen).

妄司 2024-12-26 14:07:52

另一种稍微不同的方法是使用媒体查询并回退到window.document.fullscreenElement

无论是点击/触摸事件还是按下 F11 键,这都适用于 Chrome。

function fullscreenEvent(fullscreen) {
    ...
}

window.onresize = function () {
    if (window.matchMedia('(display-mode: fullscreen)').matches ||
    window.document.fullscreenElement) {
       fullscreenEvent(true);
    } else {
       fullscreenEvent(false);
    }
}

A slightly other approach using a media query and a fallback to the window.document.fullscreenElement.

This works on Chrome whether it's a click/touch event or the F11 key being pressed.

function fullscreenEvent(fullscreen) {
    ...
}

window.onresize = function () {
    if (window.matchMedia('(display-mode: fullscreen)').matches ||
    window.document.fullscreenElement) {
       fullscreenEvent(true);
    } else {
       fullscreenEvent(false);
    }
}
且行且努力 2024-12-26 14:07:52

有一个可用于 jQuery 的插件(我知道你没有使用 jQuery).....什么它的作用是监听窗口上按下的按键 - 所以它会监听按下 F11 等...不是最好的解决方案,但可能会起作用。

除此之外,我认为您被难住了...

一个想法...

我只是偶然发现了这个页面 -> http://www.javascriptkit.com/howto/newtech3.shtml

JavaScript 可以检测使用 screen.width / screen.height 的屏幕大小...也许使用调整大小事件来查看浏览器是否与屏幕大小(即全屏)匹配?

@Nathans 的答案正是我所说的......

There is a plugin available for jQuery ( i know your not using jQuery ) ..... what it does is listen to the keys pressed on the window - so it listens for F11 being pressed etc ... Not the greatest solution but one that might work

Short of that I think you are stumped ...

A thought ...

I just stumbled across this page -> http://www.javascriptkit.com/howto/newtech3.shtml

JavaScript can detect the size of the screen using screen.width / screen.height ... perhaps use the resize event to see if the browser matches the screen size ie fullscreen ?

@Nathans answer is exactly what i was talking about ...

弥枳 2024-12-26 14:07:52

有一个用于 Javascript 的建议的全屏 API,它允许您连接到 onfullscreenchange 事件。

然而,目前我对浏览器支持不是很乐观。

There is a proposed Fullscreen API for Javascript, which would allow you to hook into the onfullscreenchange event.

However, I 'm not very optimistic with regards to browser support at this point in time.

神爱温柔 2024-12-26 14:07:52

使用这个 jQuery 插件 是的

,我知道可能有一种纯 js 的方式,但这是很容易。

how about using this jQuery plugin

yes, I know there might be an pure js way, but this is very easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文