AMP

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

amp-list

描述

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

 

必要指令碼

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

用法

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

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

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

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

在搭配另一個範本 AMP 元件 (例如 <amp-form>) 使用 <amp-list> 時,請注意範本可能不會在有效的 AMP 文件中巢狀。在這種情況下,有效的替代方案是透過 template 屬性依 id 提供範本。進一步瞭解 <amp-mustache> 中的巢狀範本

如需範本的更多詳細資訊,請參閱 AMP HTML 範本

顯示動態清單

在以下範例中,我們擷取包含網址和標題的 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 角色新增至透過範本呈現的項目元素。如果清單元素或其任何子項不是「可定位焦點的」(可透過鍵盤按鍵 (例如 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 會批次處理對 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>
在 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 網址的資料失敗,<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-list> 且也使用 amp-bind 的頁面,控制是否要封鎖在呈現的子項中評估繫結 (例如 [text]) 的呈現。

我們建議使用 binding="no"binding="refresh" 以獲得更快的效能。

  • binding="no":永不封鎖呈現(最快)
  • binding="refresh":在初始載入時不封鎖呈現(較快)
  • binding="always":一律封鎖呈現(慢)

如果未提供 binding 屬性,則預設為 always

通用屬性

此元素包含擴充至 AMP 元件的通用屬性

驗證

請參閱 AMP 驗證器規格中的 amp-list 規則

需要更多協助嗎?

您已閱讀本文檔十幾次,但它並未真正涵蓋您的所有問題?也許其他人也有同感:請在 Stack Overflow 上與他們聯繫。

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

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

前往 GitHub