Element.requestFullscreen()
method issues an asynchronous request to make the element be displayed in full-screen mode.
It's not guaranteed that the element will be put into full screen mode. If permission to enter full screen mode is granted, the returned
Promise
will resolve and the element will receive a
fullscreenchange
event to let it know that it's now in full screen mode. If permission is denied, the promise is rejected and the element receives a
fullscreenerror
event instead. If the element has been detached from the original document, then the document receives these events instead.
Earlier implementations of the Fullscreen API would always send these events to the document rather than the element, and you may need to be able to handle that situation. Check 浏览器兼容性 in fullscreen for specifics on when each browser made this change.
注意: This method must be called while responding to a user interaction or a device orientation change; otherwise it will fail.
var promise = Element.requestFullscreen(options);
选项
可选
FullscreenOptions
object providing options that control the behavior of the transition to full-screen mode. Currently, the only option is
navigationUI
, which controls whether or not to show navigation UI while the element is in full-screen mode. The default value is
"auto"
, which indicates that the browser should decide what to do.
A
Promise
which is resolved with a value of
undefined
when the transition to full screen is complete.
Rather than throw a traditional exception, the
requestFullscreen()
procedure announces error conditions by rejecting the
Promise
it has returned. The rejection handler receives one of the following exception values:
TypeError
TypeError
exception may be delivered in any of the following situations:
"fullscreen"
feature, either because of Feature Policy configuration or other access control features.
An element that you wish to place into full-screen mode has to meet a small number of simple requirements:
<svg>
or
<math>
.
<dialog>
元素。
<iframe>
which has the
allowfullscreen
attribute applied to it.
Additionally, of course, the Feature Policy
"fullscreen"
permission must be granted.
You can determine whether or not your attempt to switch to full-screen mode is successful by using the
Promise
返回通过
requestFullscreen()
, as seen in the
范例
下文。
To learn when other code has toggled full-screen mode on and off, you should establish listeners for the
fullscreenchange
event on the
Document
. It's also important to listen for
fullscreenchange
to be aware when, for example, the user manually toggles full-screen mode, or when the user switches applications, causing your application to temporarily exit full-screen mode.
This function toggles the first
<video>
element found in the document into and out of full-screen mode.
function toggleFullscreen() {
let elem = document.querySelector("video");
if (!document.fullscreenElement) {
elem.requestFullscreen().catch(err => {
alert(`Error attempting to enable full-screen mode: ${err.message} (${err.name})`);
});
} else {
document.exitFullscreen();
}
}
If the document isn't already in full-screen mode—detected by looking to see if
document.fullscreenElement
has a value—we call the video's
requestFullscreen()
method. We don't need to do anything special if successful, but if the request fails, our promise's
catch()
handler presents an alert with an appropriate error message.
If, on the other hand, full-screen mode is already in effect, we call
document.exitFullscreen()
to disable full-screen mode.
You can see this example in action or view or remix the code on Glitch .
| 规范 | 状态 | 注释 |
|---|---|---|
|
全屏 API
The definition of 'Element.requestFullscreen()' in that specification. |
实时标准 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
requestFullscreen
|
Chrome
69
|
Edge
79
|
Firefox
64
|
IE
11
Prefixed
|
Opera
58
|
Safari
6
Prefixed
|
WebView Android
69
|
Chrome Android
69
|
Firefox Android
64
|
Opera Android
50
|
Safari iOS
6
Prefixed
注意事项
|
Samsung Internet Android
10.0
|
选项
参数
|
Chrome 71 | Edge 79 | Firefox 64 | IE 不支持 No | Opera ? | Safari ? | WebView Android 71 | Chrome Android 71 | Firefox Android 64 | Opera Android ? | Safari iOS ? | Samsung Internet Android 10.0 |
返回
Promise
|
Chrome 71 | Edge 79 | Firefox 64 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 71 | Chrome Android 71 | Firefox Android 64 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 10.0 |
完整支持
不支持
兼容性未知
见实现注意事项。
用户必须明确启用此特征。
使用非标名称。
要求使用供应商前缀或不同名称。
Document.exitFullscreen()
Document.fullscreen
Document.fullscreenElement
:fullscreen
allowfullscreen
元素
accessKey
属性
childElementCount
children
classList
className
clientHeight
clientLeft
clientTop
clientWidth
currentStyle
firstElementChild
id
innerHTML
lastElementChild
localName
名称
namespaceURI
nextElementSibling
onfullscreenchange
onfullscreenerror
openOrClosedShadowRoot
outerHTML
part
prefix
previousElementSibling
runtimeStyle
scrollHeight
scrollLeft
scrollLeftMax
scrollTop
scrollTopMax
scrollWidth
shadowRoot
slot
tabStop
tagName
after()
animate()
append()
attachShadow()
before()
closest()
computedStyleMap()
createShadowRoot()
getAnimations()
getAttribute()
getAttributeNames()
getAttributeNode()
getAttributeNodeNS()
getAttributeNS()
getBoundingClientRect()
getClientRects()
getElementsByClassName()
getElementsByTagName()
getElementsByTagNameNS()
hasAttribute()
hasAttributeNS()
hasAttributes()
hasPointerCapture()
insertAdjacentElement()
insertAdjacentHTML()
insertAdjacentText()
matches()
msZoomTo()
prepend()
querySelector()
querySelector()
querySelectorAll()
querySelectorAll()
releasePointerCapture()
remove()
removeAttribute()
removeAttributeNode()
removeAttributeNS()
replaceChildren()
replaceWith()
requestFullscreen()
requestPointerLock()
scroll()
scrollBy()
scrollIntoView()
scrollIntoViewIfNeeded()
scrollTo()
setAttribute()
setAttributeNode()
setAttributeNodeNS()
setAttributeNS()
setCapture()
setPointerCapture()
toggleAttribute()
afterscriptexecute
auxclick
blur
click
compositionend
compositionstart
compositionupdate
contextmenu
copy
cut
dblclick
DOMActivate
DOMMouseScroll
error
focus
focusin
focusout
fullscreenchange
fullscreenerror
gesturechange
gestureend
gesturestart
keydown
keypress
keyup
mousedown
mouseenter
mouseleave
mousemove
mouseout
mouseover
mouseup
mousewheel
MozMousePixelScroll
msContentZoom
MSGestureChange
MSGestureEnd
MSGestureHold
MSGestureStart
MSGestureTap
MSInertiaStart
MSManipulationStateChanged
overflow
paste
scroll
select
show
touchcancel
touchend
touchmove
touchstart
underflow
webkitmouseforcechanged
webkitmouseforcedown
webkitmouseforceup
webkitmouseforcewillbegin
wheel