AMP
  • 故事

AMP 中的故事 - Hello World

簡介

瞭解如何建立您的第一個 AMP 故事。amp-story 擴充功能為顯示視覺內容提供新的格式。

設定

AMP 故事是使用 AMPHTML 撰寫的,並且使用它們自己的 AMP 擴充功能:amp-story。第一步是在標頭中匯入 amp-story

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

AMP 故事可以使用其他 AMP 擴充功能,例如 amp-video。但是,AMP 故事僅支援可用 AMP 擴充功能的子集。您可以在此處找到支援的擴充功能完整清單。

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

故事可以使用 CSS 設定樣式

<style amp-custom>
  amp-story {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI ", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji ", "Segoe UI Emoji ", "Segoe UI Symbol ";
  }
  amp-story-page * {
    color: white;
    text-align: center;
  }
  [template=thirds] {
    padding: 0;
  }
</style>

撰寫故事

故事在 amp-story 標籤內宣告。故事情節層級的中繼資料透過屬性提供。(前四個屬性是必填,最後兩個是選填,但建議使用。)請注意,此中繼資料擴充但不會取代 結構化資料

<amp-story standalone
  title="Stories in AMP - Hello World"
  publisher="AMP Project"
  publisher-logo-src="https://amp.dev.org.tw/favicons/coast-228x228.png"
  poster-portrait-src="https://amp.dev.org.tw/static/samples/img/story_dog2_portrait.jpg"
  poster-square-src="https://amp.dev.org.tw/static/samples/img/story_dog2_square.jpg"
  poster-landscape-src="https://amp.dev.org.tw/static/samples/img/story_dog2_landscape.jpg">

一個故事由一或多個頁面組成。每個頁面都由 amp-story-page 元素宣告。頁面透過分層影片、圖片和文字來設計。這裡我們有一個使用兩個圖層的頁面。一個圖層用圖片填滿可用空間,另一個文字圖層顯示標題。

<amp-story-page id="page-1">
  <amp-story-grid-layer template="fill">
    <amp-img src="https://amp.dev.org.tw/static/samples/img/story_dog2.jpg"
             width="720" height="1280"
             layout="responsive">
    </amp-img>
  </amp-story-grid-layer>
  <amp-story-grid-layer template="vertical">
    <h1>Hello World</h1>
    <p>This is an AMP Story.</p>
  </amp-story-grid-layer>
</amp-story-page>

這裡我們有一個由自動播放和循環播放的影片組成的頁面。

<amp-story-page id="page-2">
  <amp-story-grid-layer template="fill">
    <amp-video autoplay loop
          width="720"
          height="960"
          poster="https://amp.dev.org.tw/static/samples/img/story_video_dog_cover.jpg"
          layout="responsive">
          <source src="https://amp.dev.org.tw/static/samples/video/story_video_dog.mp4" type="video/mp4">
    </amp-video>
  </amp-story-grid-layer>
</amp-story-page>

預先定義的進入動畫可以不用影片就能建立動態頁面。長度和初始延遲可以使用 animate-in-durationanimate-in-delay 屬性自訂。動畫範例顯示所有可用的動畫。

<amp-story-page id="animation-demo">
  <amp-story-grid-layer template="fill">
    <amp-img src="https://amp.dev.org.tw/static/samples/img/story_dog4.jpg"
             animate-in="fly-in-top"
             width="720" height="1280"
             layout="responsive"
             alt="...">
    </amp-img>
  </amp-story-grid-layer>
  <amp-story-grid-layer template="thirds">
    <h2 animate-in="fly-in-bottom"
        grid-area="lower-third"
        animate-in-delay="0.4s">
      Best walk ever!
    </h2>
  </amp-story-grid-layer>
</amp-story-page>

故事可以使用預先定義的版面配置來設定頁面樣式。這裡我們使用 thirds 範本。如需可用版面配置的概觀,請查看版面配置範例

<amp-story-page id="layout-demo">
  <amp-story-grid-layer template="thirds">
    <amp-img grid-area="upper-third"
             src="https://amp.dev.org.tw/static/samples/img/story_thirds_1.jpg"
             width="560" height="420"
             layout="responsive"
             alt="...">
    </amp-img>
    <amp-img grid-area="middle-third"
             src="https://amp.dev.org.tw/static/samples/img/story_thirds_2.jpg"
             width="560" height="420"
             layout="responsive"
             alt="...">
    </amp-img>
    <amp-img grid-area="lower-third"
             src="https://amp.dev.org.tw/static/samples/img/story_thirds_3.jpg"
             width="560"
             height="420"
             layout="responsive"
             alt="...">
    </amp-img>
  </amp-story-grid-layer>
</amp-story-page>

如果您包含參考 bookend JSON 設定amp-story-bookend,則包含其他資源連結的「書擋」面板將會出現在故事的最後一頁。

<amp-story-bookend src="https://amp.dev.org.tw/static/samples/json/bookend.json" layout="nodisplay">
</amp-story-bookend>
需要更多說明嗎?

如果本頁面的說明沒有涵蓋您的所有問題,請隨時與其他 AMP 使用者聯繫,討論您的確切使用案例。

前往 Stack Overflow
有未說明的特色功能嗎?

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

在 GitHub 上編輯範例