This article demonstrates how to use the 全屏 API to place a given element into full-screen mode, as well as how to detect when the browser enters or exits full-screen mode.

Activating full-screen mode

Given an element that you'd like to present in full-screen mode (such as a <video> , for example), you can present it in full-screen mode by simply calling its requestFullscreen() 方法。

Let's consider this <video> 元素:

<video controls id="myvideo">
  <source src="somevideo.webm"></source>
  <source src="somevideo.mp4"></source>
</video>
					

We can put that video into full-screen mode as follows:

var elem = document.getElementById("myvideo");
if (elem.requestFullscreen) {
  elem.requestFullscreen();
}
					

This code checks for the existence of the requestFullscreen() method before calling it.

Presentation differences

It's worth noting a key difference here between the Gecko and WebKit implementations at this time: Gecko automatically adds CSS rules to the element to stretch it to fill the screen: " width: 100%; height: 100% ". WebKit doesn't do this; instead, it centers the fullscreen element at the same size in a screen that's otherwise black. To get the same fullscreen behavior in WebKit, you need to add your own " width: 100%; height: 100%; " CSS rules to the element yourself:

#myvideo:-webkit-full-screen {
  width: 100%;
  height: 100%;
}
					

On the other hand, if you're trying to emulate WebKit's behavior on Gecko, you need to place the element you want to present inside another element, which you'll make fullscreen instead, and use CSS rules to adjust the inner element to match the appearance you want.

Notification

When fullscreen mode is successfully engaged, the document which contains the element receives a fullscreenchange event. When fullscreen mode is exited, the document again receives a fullscreenchange event. Note that the fullscreenchange event doesn't provide any information itself as to whether the document is entering or exiting fullscreen mode, but if the document has a non null fullscreenElement , you know you're in fullscreen mode.

When a fullscreen request fails

It's not guaranteed that you'll be able to switch into fullscreen mode. For example, <iframe> elements have the allowfullscreen attribute in order to opt-in to allowing their content to be displayed in fullscreen mode. In addition, certain kinds of content, such as windowed plug-ins, cannot be presented in fullscreen mode. Attempting to put an element which can't be displayed in fullscreen mode (or the parent or descendant of such an element) won't work. Instead, the element which requested fullscreen will receive a mozfullscreenerror event. When a fullscreen request fails, Firefox will log an error message to the Web Console explaining why the request failed. In Chrome and newer versions of Opera however, no such warning is generated.

注意: Fullscreen requests need to be called from within an event handler or otherwise they will be denied.

Getting out of full screen mode

The user always has the ability to exit fullscreen mode of their own accord; see Things your users want to know . You can also do so programmatically by calling the Document.exitFullscreen() 方法。

Other information

Document provides some additional information that can be useful when developing fullscreen web applications:

DocumentOrShadowRoot.fullscreenElement
fullscreenElement property tells you the 元素 that's currently being displayed fullscreen. If this is non-null, the document is in fullscreen mode. If this is null, the document is not in fullscreen mode.
Document.fullscreenEnabled
fullscreenEnabled property tells you whether or not the document is currently in a state that would allow fullscreen mode to be requested.

Things your users want to know

You'll want to be sure to let your users know that they can press the Esc key (or F11 ) to exit fullscreen mode.

In addition, navigating to another page, changing tabs, or switching to another application (using, for example, Alt - Tab ) while in fullscreen mode exits fullscreen mode as well.

范例

In this example, a video is presented in a web page. Pressing the 返回 or Enter key lets the user toggle between windowed and fullscreen presentation of the video.

View Live Examples

Watching for the Enter key

When the page is loaded, this code is run to set up an event listener to watch for the Enter key.

document.addEventListener("keydown", function(e) {
  if (e.keyCode == 13) {
    toggleFullScreen();
  }
}, false);
					

Toggling fullscreen mode

This code is called when the user hits the Enter key, as seen above.

function toggleFullScreen() {
  if (!document.fullscreenElement) {
    document.documentElement.requestFullscreen();
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    }
  }
}
					

This starts by looking at the value of the fullscreenElement 属性在 document (checking it prefixed with both moz , ms ,或 webkit ). If it's null , the document is currently in windowed mode, so we need to switch to fullscreen mode. Switching to fullscreen mode is done by calling element.requestFullscreen() .

If fullscreen mode is already active ( fullscreenElement 为非 null ), we call document.exitFullscreen() .

Prefixing

注意: Currently, only Firefox 64 and Chrome 71 supports this unprefixed.

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 ). Here is the table summarizing the prefixes and name differences between them:

标准 WebKit (Safari) / Blink (Chrome & Opera) / Edge Gecko (Firefox) Internet Explorer
Document.fullscreen webkitIsFullScreen mozFullScreen -
Document.fullscreenEnabled webkitFullscreenEnabled mozFullScreenEnabled msFullscreenEnabled
DocumentOrShadowRoot.fullscreenElement webkitFullscreenElement mozFullScreenElement msFullscreenElement
Document.onfullscreenchange onwebkitfullscreenchange onmozfullscreenchange onmsfullscreenchange
Document.onfullscreenerror onwebkitfullscreenerror onmozfullscreenerror onmsfullscreenerror
Document.exitFullscreen() webkitExitFullscreen() mozCancelFullScreen() msExitFullscreen()
Element.requestFullscreen() webkitRequestFullscreen() mozRequestFullScreen() msRequestFullscreen()

规范

规范 状态 注释
全屏 API 实时标准 Initial version.

浏览器兼容性

Document.fullscreen

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
fullscreen 弃用 Chrome 71
71
15 Alternate Name
Alternate Name Uses the non-standard name: webkitIsFullscreen
Edge ≤79
≤79
≤79 Alternate Name
Alternate Name Uses the non-standard name: webkitIsFullscreen
Firefox 64
64
49 — 65 Disabled
Disabled From version 49 until version 65 (exclusive): this feature is behind the full-screen-api.unprefix.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
9 — 65 Alternate Name
Alternate Name Uses the non-standard name: mozFullScreen
IE 不支持 No Opera 58
58
15 Alternate Name
Alternate Name Uses the non-standard name: webkitIsFullscreen
Safari 6 Alternate Name
6 Alternate Name
Alternate Name Uses the non-standard name: webkitIsFullScreen
WebView Android 71
71
≤37 Alternate Name
Alternate Name Uses the non-standard name: webkitIsFullscreen
Chrome Android 71
71
18 Alternate Name
Alternate Name Uses the non-standard name: webkitIsFullscreen
Firefox Android 64
64
49 — 65 Disabled
Disabled From version 49 until version 65 (exclusive): this feature is behind the full-screen-api.unprefix.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
9 — 65 Alternate Name
Alternate Name Uses the non-standard name: mozFullScreen
Opera Android 50
50
14 Alternate Name
Alternate Name Uses the non-standard name: webkitIsFullscreen
Safari iOS 6 Alternate Name
6 Alternate Name
Alternate Name Uses the non-standard name: webkitIsFullScreen
Samsung Internet Android 10.0
10.0
1.0 Alternate Name
Alternate Name Uses the non-standard name: webkitIsFullscreen

图例

完整支持

完整支持

不支持

不支持

弃用。不要用于新网站。

弃用。不要用于新网站。

用户必须明确启用此特征。

用户必须明确启用此特征。

使用非标名称。

使用非标名称。

Document.fullscreenEnabled

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
fullscreenEnabled Chrome 45
45
Yes Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 64
64
不支持 47 — 65 Disabled
Disabled From version 47 until version 65 (exclusive): this feature is behind the full-screen-api.unprefix.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 10 — 65 Alternate Name
Alternate Name Uses the non-standard name: mozFullScreenEnabled
IE 11 Alternate Name
11 Alternate Name
Alternate Name Uses the non-standard name: msFullScreenEnabled
Opera Yes Safari ? WebView Android 45
45
Yes Prefixed
Prefixed Implemented with the vendor prefix: webkit
Chrome Android 45
45
Yes Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android 64
64
不支持 47 — 65 Disabled
Disabled From version 47 until version 65 (exclusive): this feature is behind the full-screen-api.unprefix.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 10 — 65 Alternate Name
Alternate Name Uses the non-standard name: mozFullScreenEnabled
Opera Android Yes Safari iOS ? Samsung Internet Android 5.0
5.0
Yes Prefixed
Prefixed Implemented with the vendor prefix: webkit

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

用户必须明确启用此特征。

用户必须明确启用此特征。

使用非标名称。

使用非标名称。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改:
  1. 全屏 API
  2. 指南
    1. 全屏 API 指南
  3. 特性
    1. Document.fullscreen
    2. DocumentOrShadowRoot.fullscreenElement
    3. Document.onfullscreenchange
    4. Document.onfullscreenerror
    5. Element.onfullscreenchange
    6. Element.onfullscreenerror
  4. 方法
    1. Document.exitFullscreen()
    2. Element.requestFullscreen()
  5. 事件
    1. Document : fullscreenchange
    2. Document : fullscreenerror
    3. 元素 : fullscreenchange
    4. 元素 : fullscreenerror

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1