amp-list
說明
動態下載資料並使用範本建立列表項目。
必要指令碼
<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-1.0.js"></script>
支援的版面配置
用法
amp-list
元件會從 CORS JSON 端點擷取動態內容。端點的回應包含資料,這些資料會在指定的範本中呈現。
您可以使用以下兩種方式之一指定範本
template
屬性,參照現有範本元素的 ID。- 直接巢狀於
amp-list
元素內的範本元素。
<amp-form>
) 使用 <amp-list>
時,請注意,範本可能無法在有效的 AMP 文件中巢狀。在這種情況下,有效的替代方案是透過 template
屬性以 id
提供範本。深入瞭解 <amp-mustache>
中的巢狀範本。如需範本的更多詳細資訊,請參閱 AMP HTML 範本。
顯示動態列表
在以下範例中,我們擷取包含 URL 和標題的 JSON 資料,並在巢狀 amp-mustache 範本中呈現內容。
<amp-list width="auto" height="100" layout="fixed-height" src="/static/inline-examples/data/amp-list-urls.json" > <template type="amp-mustache"> <div class="url-entry"> <a href="{{url}}">{{title}}</a> </div> </template> </amp-list>
以下是我們使用的 JSON 檔案
{ "items": [ { "title": "AMP YouTube Channel", "url": "https://www.youtube.com/channel/UCXPBsjgKKG2HqsKBhWA4uQw" }, { "title": "AMPproject.org", "url": "https://www.ampproject.org/" }, { "title": "AMP", "url": "https://amp.dev.org.tw/" }, { "title": "AMP Start", "url": "https://ampstart.com/" } ] }
以下是我們如何設定擷取內容的樣式
amp-list div[role='list'] { display: grid; grid-gap: 0.5em; }
請求始終從用戶端發出,即使文件是從 AMP 快取伺服器提供的也是如此。載入是使用正常的 AMP 規則觸發的,具體取決於元素與目前視窗的距離。
如果 <amp-list>
在載入後需要更多空間,它會請求 AMP 執行階段使用正常的 AMP 流程更新其高度。如果 AMP 執行階段無法滿足對新高度的請求,則會在可用時顯示 overflow
元素。但是請注意,<amp-list>
元素在文件底部的典型放置幾乎總是能保證 AMP 執行階段可以調整它們的大小。
amp-list
的無障礙考量
預設情況下,<amp-list>
會將 list
ARIA 角色新增至列表元素,並將 listitem
角色新增至透過範本呈現的項目元素。如果列表元素或其任何子元素不是「可 Tab 鍵瀏覽的」(可透過鍵盤按鍵 (例如 a
和 button
元素或任何具有正 tabindex
的元素) 存取),則預設會將 tabindex
0
新增至列表項目。這種行為不一定總是適當的 - 一般來說,只有互動式控制項/內容才應該是可聚焦的。如果您想要抑制此行為,請確保將 tabindex="-1"
作為範本最外層元素的一部分包含在內。
aria-live="polite"
),這表示列表內容的任何變更都會導致輔助技術 (例如螢幕閱讀器) 讀出/宣告整個列表。由於列表最初呈現的方式,這也可能導致在頁面載入時完整宣告列表。為了暫時解決此問題,您可以將 aria-live="off"
新增至 <amp-list>
,這將覆寫 aria-live="polite"
的新增。
<template type="amp-mustache"> <div class="item">{{item}}</div> <div class="price">{{price}}</div> </template>
如果改為以下方式提供,則最可預測地套用和呈現
<template type="amp-mustache"> <div> <div class="item">{{item}}</div> <div class="price">{{price}}</div> </div> </template>
XHR 批次處理
AMP 會批次處理對 JSON 端點的 XMLHttpRequests (XHR),也就是說,您可以使用單一 JSON 資料請求作為 AMP 頁面上多個消費者 (例如,多個 <amp-list>
元素) 的資料來源。例如,如果您的 <amp-list>
對端點發出 XHR,當 XHR 正在傳輸中時,對相同端點的所有後續 XHR 都將不會觸發,而是會傳回來自第一個 XHR 的結果。
在 <amp-list>
中,您可以使用 items
屬性來呈現 JSON 回應的子集,讓您可以讓多個 <amp-list>
元素呈現不同的內容,但共用單一 XHR。
指定溢位行為
或者,<amp-list>
元件可以包含具有 overflow
屬性的元素。如果滿足以下所有條件,AMP 將顯示此元素
- 呈現到
amp-list
中的內容超出其指定的大小。 amp-list
的底部位於視窗內。amp-list
的底部不在頁面底部附近 (定義為文件底部 15% 或底部 1000 像素的最小值)
如果 amp-list
在視窗外,它將自動展開。
範例:當列表需要更多空間時顯示溢位
在以下範例中,我們顯示影像和標題的列表。由於 <amp-list>
內容需要比可用空間更多的空間,因此 AMP 框架會顯示 overflow 元素。
<amp-list width="auto" height="140" layout="fixed-height" src="/static/inline-examples/data/amp-list-data.json" > <template type="amp-mustache"> <div class="image-entry"> <amp-img src="{{imageUrl}}" width="100" height="75"></amp-img> <span class="image-title">{{title}}</span> </div> </template> <div overflow class="list-overflow" style="background-color:red;"> See more </div> </amp-list>
AMP 將以下 CSS 套用至具有 overflow
屬性的元素
.list-overflow[overflow] { position: absolute; bottom: 0; left: 0; right: 0; }
預留位置和備用內容
或者,<amp-list>
支援預留位置和/或備用內容。
- 預留位置是具有
placeholder
屬性的子元素。此元素會顯示,直到<amp-list>
成功載入為止。如果也提供了備用內容,則當<amp-list>
無法載入時,預留位置會隱藏。 - 備用內容是具有
fallback
屬性的子元素。如果<amp-list>
無法載入,則會顯示此元素。
在 預留位置和備用內容中瞭解更多資訊。請注意,子元素不能同時是預留位置和備用內容。
<amp-list src="https://foo.com/list.json"> <div placeholder>Loading ...</div> <div fallback>Failed to load data.</div> </amp-list>
重新整理資料
<amp-list>
元素公開了一個 refresh
動作,其他元素可以在 on="tap:..."
屬性中參照該動作。
<button on="tap:myList.refresh">Refresh List</button> <amp-list id="myList" src="https://foo.com/list.json"> <template type="amp-mustache"> <div>{{title}}</div> </template> </amp-list>
動態調整大小
在某些情況下,我們可能需要 <amp-list>
在使用者互動時調整大小。例如,當 <amp-list>
包含使用者可能會點擊的 amp-accordion 時,當 <amp-list>
的內容由於繫結的 CSS 類別而變更大小時。changeToLayoutContainer
動作會透過在觸發此動作時將 amp list 變更為 layout="CONTAINER"
來處理此問題。請參閱以下範例
<button on="tap:list.changeToLayoutContainer()">Show Grid</button> <amp-list id="list" width="396" height="80" layout="responsive" src="/test/manual/amp-list-data.json?RANDOM" > <template type="amp-mustache"> {{title}} </template> </amp-list>
屬性
src
(必填)
遠端端點的 URL,該端點會傳回 JSON 以供 amp-list 呈現。這必須參照 CORS HTTP 服務,且不支援不安全的 HTTP。
如果擷取 src
URL 上的資料失敗,<amp-list>
會觸發低信任度的 fetch-error
事件。
items
定義運算式以尋找要在回應中呈現的陣列。這是一個以點分隔的運算式,透過 JSON 回應的欄位導覽。依預設,<amp-list>
預期為陣列,可以使用 single-item
屬性從物件載入資料。
- 預設值為
"items"
。預期的回應:{items: [...]}
。 - 如果回應本身是所需的陣列,請使用值
"."
。預期的回應為:[...]
。 - 允許巢狀導覽 (例如,
"field1.field2"
)。預期的回應為:{field1: {field2: [...]}}
。
當指定 items="items"
(這是預設值) 時,回應必須是包含名為 "items"
的陣列屬性的 JSON 物件
{ "items": [...] }
max-items
整數值,指定要呈現的項目陣列的最大長度。如果傳回的值超過 max-items
,則會將 items
陣列截斷為 max-items
個項目。
single-item
使 <amp-list>
將傳回的結果視為單一元素陣列。物件回應將包裝在陣列中,因此 {items: {...}}
的行為就像 {items: [{...}]}
一樣。
binding
對於也使用 amp-bind
的 <amp-list>
頁面,控制是否在呈現子項中評估繫結 (例如 [text]
) 時封鎖呈現。
我們建議使用 binding="no"
或 binding="refresh"
以獲得更快的效能。
binding="no"
:永遠不要封鎖呈現(最快)。binding="refresh"
:在初始載入時不要封鎖呈現(較快)。binding="always"
:永遠封鎖呈現(慢)。
如果未提供 binding
屬性,則預設值為 always
。
通用屬性
此元素包含 擴充至 AMP 元件的通用屬性。
無效的 AMP 電子郵件屬性
AMP for Email 規格不允許在 AMP 電子郵件格式中使用以下屬性。
[src]
[state]
[is-layout-container]
auto-resize
credentials
data-amp-bind-src
load-more
load-more-bookmark
reset-on-refresh
xssi-prefix
驗證
請參閱 AMP 驗證器規格中的 amp-list 規則。
您已經閱讀這份文件十幾次了,但它仍然沒有真正涵蓋您的所有問題?也許其他人也有同樣的感覺:在 Stack Overflow 上與他們聯繫。
前往 Stack Overflow 發現錯誤或缺少功能?AMP 專案強烈鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的持續參與者,但我們也歡迎針對您特別熱衷的問題的一次性貢獻。
前往 GitHub