AMP

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

amp-carousel

說明

沿著水平軸顯示多個相似的內容片段。

 

必要指令碼

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

用法

用於沿著水平軸顯示多個相似內容片段的通用輪播;旨在提供彈性和高效能。

amp-carousel 元件的所有直接子項都會被視為輪播中的項目。這些節點的每一個也可能具有任意 HTML 子項。

輪播包含任意數量的項目,以及可選的導覽箭頭以前進或後退。對於 type="slides",箭頭一次移動一個項目。對於 type="carousel",箭頭一次移動一個輪播的寬度向前或向後。

如果使用者滑動或點擊可選的導覽箭頭,輪播會在項目之間前進。

<amp-carousel
  width="450"
  height="300"
  layout="responsive"
  type="slides"
  role="region"
  aria-label="Basic carousel"
>
  <amp-img
    src="/static/inline-examples/images/image1.jpg"
    width="450"
    height="300"
  ></amp-img>
  <amp-img
    src="/static/inline-examples/images/image2.jpg"
    width="450"
    height="300"
  ></amp-img>
  <amp-img
    src="/static/inline-examples/images/image3.jpg"
    width="450"
    height="300"
  ></amp-img>
</amp-carousel>
在 Playground 中開啟此程式碼片段

<amp-carousel> 0.1 的差異

  • autoplay 允許用於 type="carousel"
  • loop 允許用於 type="carousel"

移轉注意事項

  • 將必要指令碼從 amp-carousel-0.1 更新為 amp-carousel-0.2
  • 確保用於定位下一個/上一個箭頭的任何 CSS 仍然有效。請參閱樣式設定以取得關於箭頭定位的更多資訊。
  • 確保用於設定輪播樣式的任何 CSS 仍然有效。<amp-carousel> 0.2 的內部 DOM 結構與 0.1 不同,會影響以內部元素 (例如 amp-carousel > div) 為目標的 CSS 選取器。任何使用 .amp-class-name 格式的選取器應該仍然有效。
  • 注意:對 amp-carousel-0.1 的支援有限,且計畫在未來棄用。

進階到特定投影片

在元素的 on 屬性上設定 tap:carousel-id.goToSlide(index=N) 方法,將會在使用者輕觸或點擊時,將 ID 為 "carousel-id" 的輪播前進到索引為 N 的投影片 (第一張投影片的索引為 0,第二張投影片的索引為 1,依此類推)。

在以下範例中,我們有一個包含三張圖片的輪播,輪播下方有預覽按鈕。當使用者點擊其中一個按鈕時,會顯示對應的輪播項目。

<amp-carousel
  id="carousel-with-preview"
  width="450"
  height="300"
  layout="responsive"
  type="slides"
  role="region"
  aria-label="Carousel with slide previews"
>
  <amp-img
    src="/static/inline-examples/images/image1.jpg"
    width="450"
    height="300"
    layout="responsive"
    alt="apples"
  ></amp-img>
  <amp-img
    src="/static/inline-examples/images/image2.jpg"
    width="450"
    height="300"
    layout="responsive"
    alt="lemons"
  ></amp-img>
  <amp-img
    src="/static/inline-examples/images/image3.jpg"
    width="450"
    height="300"
    layout="responsive"
    alt="blueberries"
  ></amp-img>
</amp-carousel>
<div class="carousel-preview">
  <button on="tap:carousel-with-preview.goToSlide(index=0)">
    <amp-img
      src="/static/inline-examples/images/image1.jpg"
      width="60"
      height="40"
      alt="apples"
    ></amp-img>
  </button>
  <button on="tap:carousel-with-preview.goToSlide(index=1)">
    <amp-img
      src="/static/inline-examples/images/image2.jpg"
      width="60"
      height="40"
      alt="lemons"
    ></amp-img>
  </button>
  <button on="tap:carousel-with-preview.goToSlide(index=2)">
    <amp-img
      src="/static/inline-examples/images/image3.jpg"
      width="60"
      height="40"
      alt="blueberries"
    ></amp-img>
  </button>
</div>
在 Playground 中開啟此程式碼片段

自動播放,尤其是無限循環的輪播,可能會對使用者 (特別是認知障礙使用者) 造成非常大的干擾和困惑。一般來說,我們建議避免使用自動播放輪播。雖然自動播放輪播會在使用者與輪播互動後停止,但請考慮新增明確的「播放/暫停」控制項。

預設情況下,當 <amp-carousel> 呈現時,會以程式方式識別為清單 (在容器元素上使用 role="list",在每個項目上使用 role="listitem")。但是,對於 <amp-carousel type="slides">,目前未提供特定的 role。因此,當使用輔助技術的使用者讀取/瀏覽頁面並到達輪播時,這將不明顯。我們建議在 <amp-carousel> 中加入明確的 role="region" 和描述性的 aria-label (通用的 aria-label="輪播" 或更具描述性的標籤,例如 aria-label="最新新聞項目")。

目前,<amp-carousel type="slides"> 輪播宣告為 ARIA live region (使用 aria-live="polite"),這表示每次顯示新的投影片時,輔助技術 (例如螢幕閱讀器) 都會宣告投影片的完整內容。由於輪播的初始呈現方式,這也可能導致在載入頁面時宣告輪播的完整內容。這也表示包含 autoplay 輪播的頁面會在投影片自動前進時持續宣告。目前沒有針對此問題的解決方案。

屬性

type (類型)

指定輪播項目的顯示類型,可以是

  • carousel (輪播) (預設):所有投影片都會顯示且可水平捲動。每個投影片都可以使用 CSS 指定不同的寬度。
  • slides (投影片):一次顯示單張投影片,每張投影片會在使用者滑動時卡入定位。

controls (選用)

永久顯示左右箭頭,供使用者在行動裝置上導覽輪播項目。預設情況下,在行動裝置上,導覽箭頭會在使用者滑動到另一張投影片後消失。

箭頭的能見度也可以透過樣式設定來控制,並且可以使用媒體查詢來僅在特定螢幕寬度下顯示箭頭。在桌面上,除非僅存在單一子項,否則箭頭始終顯示。

data-next-button-aria-label (選用)

設定 amp-carousel-button-next 的 aria-label。如果未提供值,aria-label 預設為「輪播中的下一個項目」。

data-prev-button-aria-label (選用)

設定 amp-carousel-button-prev 的 aria-label。如果未提供值,aria-label 預設為「輪播中的上一個項目」。

data-button-count-format (選用)

格式字串,看起來像 (%s of %s),用作 amp-carousel-button-next/amp-carousel-button-prev 的 aria-label 的後綴。這會向使用螢幕閱讀器的使用者提供關於他們在輪播中進度的資訊。如果未提供值,則預設為 (%s of %s)

autoplay (選用)

定期前進到下一張投影片,無需使用者互動。如果使用者手動變更投影片,則自動播放會停止。

如果存在但沒有值

  • 預設情況下,以 5000 毫秒間隔 (5 秒) 前進一張投影片;這可以被 delay 屬性覆寫。
  • 需要至少 2 張投影片才能進行自動播放。

如果存在值

  • 在完成所需的循環次數後停止自動播放。

delay (選用)

指定啟用 autoplay 時延遲前進到下一張投影片的持續時間 (以毫秒為單位)。請注意,延遲的最小允許值為 1000 毫秒。

loop (選用)

允許使用者前進超過第一個項目或最後一個項目。必須至少有 3 張投影片才能進行循環播放。

以下範例顯示具有控制項、循環播放和延遲自動播放的投影片輪播。

<amp-carousel type="slides"
  width="450"
  height="300"
  controls
  loop
  autoplay
  delay="3000"  data-next-button-aria-label="Go to next slide"
  data-previous-button-aria-label="Go to previous slide"
  role="region"
  aria-label="Looping carousel">
  <amp-img src="/static/inline-examples/images/image1.jpg"
    width="450"
    height="300"></amp-img>
  <amp-img src="/static/inline-examples/images/image2.jpg"
    width="450"
    height="300"></amp-img>
  <amp-img src="/static/inline-examples/images/image3.jpg"
    width="450"
    height="300"></amp-img>
</amp-carousel>
在 Playground 中開啟此程式碼片段

slide (選用)

指定輪播首次呈現時應顯示的索引。這可以使用 amp-bind 進行更新,以變更顯示的索引。

通用屬性

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

動作

goToSlide(index=INTEGER)

將輪播前進到指定的投影片索引。

toggleAutoplay(toggleOn=true|false)

切換輪播的自動播放狀態。toggleOn 是選用的。

活動

slideChange (投影片變更)

當輪播的目前投影片變更時觸發。

// Slide number.
Event.index

樣式設定

  • 您可以使用 amp-carousel 元素選取器來自由設定其樣式。
  • 您可以使用 .amp-carousel-slide 類別選取器來鎖定輪播項目。
  • amp-carousel 按鈕在停用時的視覺狀態為隱藏。
  • 預設情況下,.amp-carousel-button 使用內嵌 SVG 作為按鈕的背景圖片。您可以使用自己的圖片覆寫此設定。某些電子郵件用戶端可能不支援 SVG。

範例:覆寫預設 .amp-carousel-button 內嵌 SVG

.amp-carousel-button-prev {
  background-image: url('http://example.com/arrow.png');
}

您可以使用 align-self 和/或相對定位來定位輪播按鈕。請注意,輪播箭頭會在 RTL 中自動翻轉,因此您不應變更其 flex 順序。

.amp-carousel-button-prev {
  position: relative;
  bottom: 20px;
  align-self: flex-end;
}

驗證

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

需要更多協助嗎?

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

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

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

前往 GitHub