瞭解建議的 Portals API 如何改善導覽使用者體驗。
確保網頁快速載入,是提供良好使用者體驗的關鍵。但我們經常忽略一個領域,那就是頁面轉換,也就是使用者在頁面之間移動時看到的內容。
新的網頁平台 API 提案「Portals」旨在簡化使用者在網站中瀏覽的體驗,協助解決這個問題。
查看入口網站的實際運作情形:
入口網站的功能
單頁應用程式 (SPA) 提供良好的轉場效果,但建構成本較高。多頁應用程式 (MPA) 的建構難度較低,但頁面之間會出現空白畫面。
入口網站結合了兩者的優點:MPA 的低複雜度,以及 SPA 的流暢轉場效果。您可以將其視為 <iframe>,因為它們允許嵌入,但與 <iframe> 不同的是,它們還具有導航到其內容的功能。
眼見為憑:請先查看我們在 2018 年 Chrome 開發人員高峰會上展示的內容:
使用傳統導覽時,使用者必須等待空白畫面,直到瀏覽器完成轉譯目的地。使用者可透過入口網站體驗動畫,而 <portal> 則會預先算繪內容,打造流暢的瀏覽體驗。
在 Portals 推出前,我們可以使用 <iframe> 轉譯其他網頁。我們也可以加入動畫,讓頁面上的影格移動。但 <iframe> 不會讓你瀏覽其內容。入口網站可彌補這項不足,實現有趣的用途。
試用入口網站
透過 about://flags 啟用
如要在 Chrome 85 以上版本試用 Portals,請啟用實驗性旗標:
- 為同源導覽啟用
about://flags/#enable-portals旗標。 - 如要測試跨源導覽,請一併啟用
about://flags/#enable-portals-cross-origin旗標。
在 Portals 實驗的早期階段,我們也建議您設定 --user-data-dir 指令列旗標,使用完全獨立的使用者資料目錄進行測試。啟用入口網站後,請在開發人員工具中確認您有全新的 HTMLPortalElement。

實作入口網站
我們來看看基本的實作範例。
// Create a portal with the wikipedia page, and embed it
// (like an iframe). You can also use the <portal> tag instead.
portal = document.createElement('portal');
portal.src = 'https://en.wikipedia.org/wiki/World_Wide_Web';
portal.style = '...';
document.body.appendChild(portal);
// When the user touches the preview (embedded portal):
// do fancy animation, e.g. expand …
// and finish by doing the actual transition.
// For the sake of simplicity, this snippet will navigate
// on the `onload` event of the Portals element.
portal.addEventListener('load', (evt) => {
portal.activate();
});
就是這麼簡單。在開發人員工具控制台中試用這段程式碼,應該會開啟維基百科頁面。

如果您想建構類似我們在 Chrome 開發人員高峰會上展示的內容,且運作方式與上述示範相同,請參考下列程式碼片段。
// Adding some styles with transitions
const style = document.createElement('style');
style.innerHTML = `
portal {
position:fixed;
width: 100%;
height: 100%;
opacity: 0;
box-shadow: 0 0 20px 10px #999;
transform: scale(0.4);
transform-origin: bottom left;
bottom: 20px;
left: 20px;
animation-name: fade-in;
animation-duration: 1s;
animation-delay: 2s;
animation-fill-mode: forwards;
}
.portal-transition {
transition: transform 0.4s;
}
@media (prefers-reduced-motion: reduce) {
.portal-transition {
transition: transform 0.001s;
}
}
.portal-reveal {
transform: scale(1.0) translateX(-20px) translateY(20px);
}
@keyframes fade-in {
0% { opacity: 0; }
100% { opacity: 1; }
}
`;
const portal = document.createElement('portal');
// Let's navigate into the WICG Portals spec page
portal.src = 'https://wicg.github.io/portals/';
// Add a class that defines the transition. Consider using
// `prefers-reduced-motion` media query to control the animation.
// https://developers.google.com/web/updates/2019/03/prefers-reduced-motion
portal.classList.add('portal-transition');
portal.addEventListener('click', (evt) => {
// Animate the portal once user interacts
portal.classList.add('portal-reveal');
});
portal.addEventListener('transitionend', (evt) => {
if (evt.propertyName == 'transform') {
// Activate the portal once the transition has completed
portal.activate();
}
});
document.body.append(style, portal);
此外,您也可以輕鬆偵測功能,使用 Portals 逐步強化網站。
if ('HTMLPortalElement' in window) {
// If this is a platform that have Portals...
const portal = document.createElement('portal');
...
}
以下示範 Portals 的運作方式:
- 輸入要預覽的網址。
- 接著,系統會將該頁面嵌入為
<portal>元素。 - 按一下預覽畫面。
- 動畫播放完畢後,預覽功能就會啟用。

查看規格
我們正在 Web Incubation Community Group (WICG) 積極討論入口網站規格。如要快速上手,請參閱一些重要情境。請務必熟悉以下三項重要功能:
<portal>元素:HTML 元素本身。這個 API 非常簡單。包含src屬性、activate函式和訊息介面 (postMessage)。activate接受選用引數,可在啟用時將資料傳遞至<portal>。portalHost介面:將portalHost物件新增至window物件。這可讓您檢查網頁是否嵌入為<portal>元素。此外,這個類別也提供將訊息傳回主機的介面 (postMessage)。- PortalActivateEvent 介面:
<portal>啟用時觸發的事件。adoptPredecessor函式相當實用,可讓您擷取上一頁做為<portal>元素。這樣一來,您就能在兩個頁面之間建立流暢的導覽和組合體驗。
接著來看看基本使用模式以外的情況。以下僅列舉部分可透過入口網站達成的目標,並提供程式碼範例。
嵌入為 <portal> 元素時自訂樣式
// Detect whether this page is hosted in a portal
if (window.portalHost) {
// Customize the UI when being embedded as a portal
}
<portal> 元素和 portalHost 之間的訊息
// Send message to the portal element
const portal = document.querySelector('portal');
portal.postMessage({someKey: someValue}, ORIGIN);
// Receive message via window.portalHost
window.portalHost.addEventListener('message', (evt) => {
const data = evt.data.someKey;
// handle the event
});
啟用 <portal> 元素並接收 portalactivate 事件
// You can optionally add data to the argument of the activate function
portal.activate({data: {somekey: 'somevalue'}});
// The portal content will receive the portalactivate event
// when the activate happens
window.addEventListener('portalactivate', (evt) => {
// Data available as evt.data
const data = evt.data;
});
擷取前置任務
// Listen to the portalactivate event
window.addEventListener('portalactivate', (evt) => {
// ... and creatively use the predecessor
const portal = evt.adoptPredecessor();
document.querySelector('someElm').appendChild(portal);
});
瞭解網頁是否已做為前置任務
// The activate function returns a Promise.
// When the promise resolves, it means that the portal has been activated.
// If this document was adopted by it, then window.portalHost will exist.
portal.activate().then(() => {
// Check if this document was adopted into a portal element.
if (window.portalHost) {
// You can start communicating with the portal element
// i.e. listen to messages
window.portalHost.addEventListener('message', (evt) => {
// handle the event
});
}
});
結合 Portals 支援的所有功能,就能打造出非常精緻的使用者體驗。舉例來說,以下示範 Portals 如何在網站和第三方嵌入內容之間,提供流暢的使用者體驗。
用途和方案
希望您喜歡這趟簡短的入口網站導覽!我們非常期待看到你的創意。舉例來說,您可能想開始使用入口網站進行非一般導覽,例如:從產品類別清單頁面預先轉譯暢銷產品的頁面。
另一個重要事項是,入口網站可用於跨源導覽,就像 <iframe> 一樣。因此,如果您有多個網站會交互參照,也可以使用入口網站,在兩個不同網站之間建立流暢的導覽體驗。這種跨來源用途是 Portals 獨有的,甚至可以改善 SPA 的使用者體驗。
歡迎踴躍提供意見
Chrome 85 以上版本已可試用傳送門。社群的意見回饋對新 API 的設計至關重要,歡迎試用並與我們分享想法!如有任何功能要求或意見回饋,請前往 WICG GitHub 存放區。