AMP

重要事項:此文件不適用於您目前選取的格式廣告

amp-list

說明

動態下載資料並使用範本建立清單項目。

 

必要指令碼

<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-1.0.js"></script>

用法

amp-list 元件從 CORS JSON 端點擷取動態內容。來自端點的回應包含資料,這些資料會在指定的範本中呈現。

您的端點必須實作AMP 中的 CORS 要求規格中指定的必要條件。

您可以使用兩種方式之一指定範本

  • 參考現有範本元素 ID 的 template 屬性。
  • 直接巢狀在 amp-list 元素內部的範本元素。

當搭配另一個範本 AMP 元件 (例如 <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>
在 Playground 中開啟此程式碼片段

以下是我們使用的 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 鍵瀏覽的」(可透過鍵盤按鍵 (例如 abutton 元素或任何具有正 tabindex 的元素) 存取),則預設會將 tabindex 0 新增至清單項目。這種行為不一定總是適當的 - 一般來說,只有互動式控制項/內容應該是可聚焦的。如果您想要抑制此行為,請務必將 tabindex="-1" 作為範本最外層元素的一部分包含在內。

目前,呈現的清單元素宣告為 ARIA 即時區域 (使用 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 會批次處理 XMLHttpRequests (XHR) 至 JSON 端點,也就是說,您可以使用單一 JSON 資料要求作為 AMP 頁面上多個消費者 (例如,多個 <amp-list> 元素) 的資料來源。例如,如果您的 <amp-list> 向端點發出 XHR,當 XHR 正在傳輸中時,所有後續對相同端點的 XHR 都不會觸發,而是會傳回來自第一個 XHR 的結果。

<amp-list> 中,您可以使用 items 屬性呈現 JSON 回應的子集,讓您可以擁有呈現不同內容但共用單一 XHR 的多個 <amp-list> 元素。

指定溢位

或者,<amp-list> 元件可以包含具有 overflow 屬性的元素。如果符合以下所有條件,AMP 將顯示此元素

  • 呈現到 amp-list 中的內容超出其指定的大小。
  • amp-list 的底部在視口內。
  • amp-list 的底部不在頁面底部附近 (定義為文件底部 15% 或底部 1000 像素的最小值)

如果 amp-list 在視口外,則會自動展開。

範例:當清單需要更多空間時顯示溢位

在以下範例中,我們顯示影像和標題的清單。由於 <amp-list> 內容需要比可用空間更多的空間,AMP 框架會顯示溢位元素。

查看更多
<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>
在 Playground 中開啟此程式碼片段

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>

動態調整大小

<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 (必填)

您的端點必須實作AMP 中的 CORS 要求規格中指定的必要條件。

如果擷取 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-itemsitems 陣列將截斷為 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-list 規則

需要更多協助嗎?

您已閱讀此文件十幾次,但它仍然沒有真正涵蓋您的所有問題嗎?也許其他人也有相同的感受:在 Stack Overflow 上與他們聯繫。

前往 Stack Overflow
發現錯誤或缺少功能?

AMP 專案強烈鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的持續參與者,但我們也歡迎針對您特別熱衷的問題的一次性貢獻。

前往 GitHub