The Picture-in-Picture API allow websites to create a floating video window always on top of other windows so that users may continue consuming media while they interact with other content sites, or applications on their device.
The Picture-in-Picture API allow websites to create a floating video window always on top of other windows so that users may continue consuming media while they interact with other content sites, or applications on their device.
PictureInPictureWindow
Represents the floating video window; contains width
and height
properties, and an onresize
event handler property.
The Picture-in-Picture API adds methods to the HTMLVideoElement
and Document
interfaces to allow toggling of the floating video window.
HTMLVideoElement.requestPictureInPicture()
Requests that the user agent enters the video into picture-in-picture mode
Document.exitPictureInPicture()
Requests that the user agent returns the element in picture-in-picture mode back into its original box.
The Picture-in-Picture API augments the HTMLVideoElement
, Document
, and ShadowRoot
interfaces with properties that can be used to determine if the floating video window mode is supported and available, if picture-in-picture mode is currently active, and which video is floating.
HTMLVideoElement.disablePictureInPicture
The disablePictureInPicture
property will provide a hint to the user agent to not suggest the picture-in-picture to users or to request it automatically.
Document.pictureInPictureEnabled
The pictureInPictureEnabled
property tells you whether or not it is possible to engage picture-in-picture mode. This is false
if picture-in-picture mode is not available for any reason (e.g. the "picture-in-picture"
feature has been disallowed, or picture-in-picture mode is not supported).
Document.pictureInPictureElement
/ ShadowRoot.pictureInPictureElement
The pictureInPictureElement
property tells you which Element
is currently being displayed in the floating window (or in the shadow DOM). If this is null
, the document (or shadow DOM) has no node currently in picture-in-picture mode.
The Picture-in-Picture API defines three events, which can be used to detect when picture-in-picture mode is toggled and when the floating video window is resized.
enterpictureinpicture
Sent to a HTMLVideoElement
when it enters picture-in-picture mode.
leavepictureinpicture
Sent to a HTMLVideoElement
when it leaves picture-in-picture mode.
resize
Sent to a PictureInPictureWindow
when it changes size.
If media action handlers have been set via the Media Session API, then appropriate controls for those actions will be added by the browser to the picture-in-picture overlay. For example, if a "nexttrack"
action has been set, then a skip button might be displayed in the picture-in-picture view. There is no support for adding custom HTML buttons or controls.
The :picture-in-picture
CSS pseudo-class matches the video element currently in picture-in-picture mode, allowing you to configure your stylesheets to automatically adjust the size, style, or layout of content when a video switches back and forth between picture-in-picture and traditional presentation modes.
The availability of picture-in-picture mode can be controlled using Permissions Policy. The fullscreen mode feature is identified by the string "picture-in-picture"
, with a default allowlist value of "self"
, meaning that picture-in-picture mode is permitted in top-level document contexts, as well as to nested browsing contexts loaded from the same origin as the top-most document.
In this example, a video is presented in a web page. Clicking the button below lets the user toggle the floating video window.
This code is called by a click handler when the user clicks the "Toggle Picture-in-Picture" button:
function togglePictureInPicture() { if (document.pictureInPictureElement) { document.exitPictureInPicture(); } else if (document.pictureInPictureEnabled) { video.requestPictureInPicture(); } }
This block starts by looking at the value of the document
's pictureInPictureElement
attribute.
If the value is not null
, it's the element that's currently in picture-in-picture mode, that is in a floating window. We call document.exitPictureInPicture()
to bring the video back into its initial box.
If the value is null
, no video is in the floating window. So we can request a video to enter the picture-in-picture mode. We do it by calling HTMLVideoElement.requestPictureInPicture()
on the <video>
element.
Specification |
---|
Picture-in-Picture # interface-picture-in-picture-window |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
Picture-in-Picture_API |
69 | 79 | No | No | 56 | 13.1 | No | 105 | No | 72 | 13.4 | 20.0 |
height |
69 | 79 | No | No | 56 | 13.1 | No | 105 | No | 72 | 13.4 | 20.0 |
resize_event |
69 | 79 | No | No | 56 | 13.1 | No | 105 | No | 72 | 13.4 | 20.0 |
width |
69 | 79 | No | No | 56 | 13.1 | No | 105 | No | 72 | 13.4 | 20.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Picture-in-Picture_API