Document Picture-in-Picture API 可讓您開啟可填入任意 HTML 內容的「一律開啟」視窗。其擴充了現有的 <video>
的子母畫面 API,這只允許將 HTML <video>
元素置於子母畫面視窗中。
Document Picture-in-Picture API 的子母畫面視窗與透過 window.open()
開啟的空白相同來源視窗類似,但仍有一些差異:
- 子母畫面視窗會懸浮在其他視窗上方。
- 子母畫面視窗絕不會超過開啟視窗。
- 無法瀏覽子母畫面視窗。
- 網站無法設定子母畫面視窗位置。
目前狀態
步驟 | 狀態 |
---|---|
1. 建立說明 | 完成 |
2. 建立規格的初始草稿 | 進行中 |
3. 收集意見回饋並改進設計 | 進行中 |
4. 來源試用 | 完成 |
5.啟動 | 完成 (電腦) |
用途
自訂影片播放器
網站可以透過現有的 <video>
專用子母畫面 API 提供子母畫面影片體驗,但實際功能有限。現有的子母畫面視窗只接受少數輸入內容,而且無法設定樣式。有了完整的子母畫面文件,網站就能提供自訂控制項和輸入資訊 (例如字幕、播放清單、時間進度控制鈕、對影片表示喜歡和不喜歡),藉此改善使用者的子母畫面觀影體驗。
視訊會議
使用者在視訊會議期間通常會基於各種原因 (例如將另一個分頁顯示在通話中或同時進行多工處理) 離開瀏覽器分頁,但仍想看到通話,因此這是子母畫面的主要用途。再次提醒,<video>
的 Picture-in-Picture API 目前提供的視訊會議網站樣式和輸入方式就有限。透過子母畫面的完整文件,網站可以輕鬆將多個影片串流合併成一個子母畫面視窗,完全不必仰賴畫布入侵,並提供自訂控制項,例如傳送訊息、忽略其他使用者或舉手等。
工作效率
研究顯示,使用者需要更多如何透過網路提升效率。子母畫面功能可讓網頁應用程式靈活完成更多工作。不論是文字編輯、筆記、工作清單、傳訊和即時通訊,還是設計與開發工具,網路應用程式現在都能讓使用者隨時存取其中的內容。
介面
屬性
documentPictureInPicture.window
- 傳回目前的子母畫面視窗 (如果有的話)。如果沒有,則傳回
null
。
方法
documentPictureInPicture.requestWindow(options)
傳回可在「子母畫面」視窗開啟時解決的承諾。承諾會拒絕在沒有使用者手勢的情況下呼叫。
options
字典包含下列選用成員:width
- 設定子母畫面視窗的初始寬度。
height
- 設定子母畫面視窗的初始高度。
disallowReturnToOpener
- 如果為 true,則會隱藏子母畫面視窗中的「返回分頁」按鈕。預設值為 false。
活動
documentPictureInPicture.onenter
- 當「子母畫面」視窗開啟時,於
documentPictureInPicture
啟動。
示例
下列 HTML 會設定自訂影片播放器和按鈕元素,以便在「子母畫面」視窗中開啟影片播放器。
<div id="playerContainer">
<div id="player">
<video id="video"></video>
</div>
</div>
<button id="pipButton">Open Picture-in-Picture window</button>
開啟子母畫面視窗
當使用者按一下按鈕開啟空白的「子母畫面」視窗時,下列 JavaScript 會呼叫 documentPictureInPicture.requestWindow()
。傳回的承諾會以「子母畫面」視窗 JavaScript 物件解析。系統會使用 append()
將影片播放器移至該視窗。
pipButton.addEventListener('click', async () => {
const player = document.querySelector("#player");
// Open a Picture-in-Picture window.
const pipWindow = await documentPictureInPicture.requestWindow();
// Move the player to the Picture-in-Picture window.
pipWindow.document.body.append(player);
});
設定子母畫面視窗的大小
如要設定子母畫面視窗的大小,請將 documentPictureInPicture.requestWindow()
的 width
和 height
選項設為所需的子母畫面視窗大小。如果選項值太大或太小,無法貼合使用者容易理解的視窗大小,Chrome 可能會限制該值。
pipButton.addEventListener("click", async () => {
const player = document.querySelector("#player");
// Open a Picture-in-Picture window whose size is
// the same as the player's.
const pipWindow = await documentPictureInPicture.requestWindow({
width: player.clientWidth,
height: player.clientHeight,
});
// Move the player to the Picture-in-Picture window.
pipWindow.document.body.append(player);
});
隱藏子母畫面視窗的「返回分頁」按鈕
如要隱藏子母畫面視窗中的按鈕,以便使用者返回開啟的分頁,請將 documentPictureInPicture.requestWindow()
的 disallowReturnToOpener
選項設為 true
。
pipButton.addEventListener("click", async () => {
// Open a Picture-in-Picture window which hides the "back to tab" button.
const pipWindow = await documentPictureInPicture.requestWindow({
disallowReturnToOpener: true,
});
});
將樣式表複製到子母畫面視窗
如要從原始視窗複製所有 CSS 樣式表,請透過 styleSheets
迴圈,與文件中明確連結或嵌入文件,然後附加至子母畫面視窗。請注意,這是一次性的複製作業。
pipButton.addEventListener("click", async () => {
const player = document.querySelector("#player");
// Open a Picture-in-Picture window.
const pipWindow = await documentPictureInPicture.requestWindow();
// Copy style sheets over from the initial document
// so that the player looks the same.
[...document.styleSheets].forEach((styleSheet) => {
try {
const cssRules = [...styleSheet.cssRules].map((rule) => rule.cssText).join('');
const style = document.createElement('style');
style.textContent = cssRules;
pipWindow.document.head.appendChild(style);
} catch (e) {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.type = styleSheet.type;
link.media = styleSheet.media;
link.href = styleSheet.href;
pipWindow.document.head.appendChild(link);
}
});
// Move the player to the Picture-in-Picture window.
pipWindow.document.body.append(player);
});
處理子母畫面視窗關閉時的處理方式
監聽視窗 "pagehide"
事件,瞭解子母畫面視窗何時關閉 (原因可能是網站啟動了這個視窗,也可能是使用者手動關閉)。事件處理常式很適合用來將元素移出子母畫面視窗,如下所示。
pipButton.addEventListener("click", async () => {
const player = document.querySelector("#player");
// Open a Picture-in-Picture window.
const pipWindow = await documentPictureInPicture.requestWindow();
// Move the player to the Picture-in-Picture window.
pipWindow.document.body.append(player);
// Move the player back when the Picture-in-Picture window closes.
pipWindow.addEventListener("pagehide", (event) => {
const playerContainer = document.querySelector("#playerContainer");
const pipPlayer = event.target.querySelector("#player");
playerContainer.append(pipPlayer);
});
});
使用 close()
方法,以程式輔助方式關閉子母畫面視窗。
// Close the Picture-in-Picture window programmatically.
// The "pagehide" event will fire normally.
pipWindow.close();
聆聽網站進入子母畫面的情況
監聽 documentPictureInPicture
上的 "enter"
事件,瞭解「子母畫面」視窗是否已開啟。事件包含 window
物件,可用來存取「子母畫面」視窗。
documentPictureInPicture.addEventListener("enter", (event) => {
const pipWindow = event.window;
});
存取子母畫面視窗中的元素
透過 documentPictureInPicture.requestWindow()
傳回的物件或使用 documentPictureInPicture.window
存取子母畫面視窗中的元素,如下所示。
const pipWindow = documentPictureInPicture.window;
if (pipWindow) {
// Mute video playing in the Picture-in-Picture window.
const pipVideo = pipWindow.document.querySelector("#video");
pipVideo.muted = true;
}
處理子母畫面視窗中的事件
建立按鈕和控制項,並回應使用者的輸入事件 (例如 "click"
),就像在 JavaScript 中一樣。
// Add a "mute" button to the Picture-in-Picture window.
const pipMuteButton = pipWindow.document.createElement("button");
pipMuteButton.textContent = "Mute";
pipMuteButton.addEventListener("click", () => {
const pipVideo = pipWindow.document.querySelector("#video");
pipVideo.muted = true;
});
pipWindow.document.body.append(pipMuteButton);
調整子母畫面視窗大小
使用 resizeBy()
和 resizeTo()
視窗方法調整子母畫面視窗大小。這兩種方法都需要使用者手勢。
const resizeButton = pipWindow.document.createElement('button');
resizeButton.textContent = 'Resize';
resizeButton.addEventListener('click', () => {
// Expand the Picture-in-Picture window's width by 20px and height by 30px.
pipWindow.resizeBy(20, 30);
});
pipWindow.document.body.append(resizeButton);
將焦點移至開啟工具視窗
使用 focus()
視窗方法,將焦點移至子母畫面視窗的開啟視窗。這個方法需要使用者手勢。
const returnToTabButton = pipWindow.document.createElement("button");
returnToTabButton.textContent = "Return to opener tab";
returnToTabButton.addEventListener("click", () => {
window.focus();
});
pipWindow.document.body.append(returnToTabButton);
CSS 子母畫面顯示模式
使用 CSS picture-in-picture
顯示模式編寫特定 CSS 規則,這些規則僅適用於以子母畫面模式顯示網頁應用程式的情況。
@media all and (display-mode: picture-in-picture) {
body {
margin: 0;
}
h1 {
font-size: 0.8em;
}
}
功能偵測
如要確認系統是否支援 Document Picture-in-Picture API,請使用:
if ('documentPictureInPicture' in window) {
// The Document Picture-in-Picture API is supported.
}
試聽帶
VideoJS 播放器
您可以透過 Document Picture-in-Picture API VideoJS Player 示範進行操作。請務必查看原始碼。
波莫多羅
pomodoro 網頁應用程式 Tomodoro 也會利用 Document Picture-in-Picture API (如果可用) 加以運用 (請參閱 GitHub 提取要求)。
意見回饋:
請在 GitHub 上提出問題,並附上建議和問題。
實用連結
特別銘謝
主頁橫幅由 Jakob Owens 提供。