AMP

熟悉入門程式碼

AMP 樣板

AMP 頁面是 HTML 頁面,但為確保效能穩定而設有一些限制。AMP 頁面有一些特殊的標記,可將其識別為 AMP 頁面。

最基本的 AMP 頁面看起來像這樣

<!DOCTYPE html>
<html amp>
  <head>
    <meta charset="utf-8" />
    <link rel="canonical" href="hello-world.html" />
    <meta name="viewport" content="width=device-width" />
    <style amp-boilerplate>
      body {
        -webkit-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
        -moz-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
        -ms-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
        animation: -amp-start 8s steps(1, end) 0s 1 normal both;
      }
      @-webkit-keyframes -amp-start {
        from {
          visibility: hidden;
        }
        to {
          visibility: visible;
        }
      }
      @-moz-keyframes -amp-start {
        from {
          visibility: hidden;
        }
        to {
          visibility: visible;
        }
      }
      @-ms-keyframes -amp-start {
        from {
          visibility: hidden;
        }
        to {
          visibility: visible;
        }
      }
      @-o-keyframes -amp-start {
        from {
          visibility: hidden;
        }
        to {
          visibility: visible;
        }
      }
      @keyframes -amp-start {
        from {
          visibility: hidden;
        }
        to {
          visibility: visible;
        }
      }
    </style>
    <noscript
      ><style amp-boilerplate>
        body {
          -webkit-animation: none;
          -moz-animation: none;
          -ms-animation: none;
          animation: none;
        }
      </style></noscript
    >
    <script async src="https://cdn.ampproject.org/v0.js"></script>
  </head>
  <body>
    Hello World!
  </body>
</html>

您可以使用樣板產生器快速設定 AMP 頁面的基本架構。它也提供結構化資料的程式碼片段,可用於建立 PWA 和更多功能!

AMP 元件

本教學課程的入門程式碼 (static/index.html static/index.html) 以最基本的 AMP 頁面為基礎,加入頁面內容 (圖片、文字等),並包含一些 AMP 元件

<script
  async
  custom-element="amp-carousel"
  src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"
></script>
<script
  async
  custom-template="amp-mustache"
  src="https://cdn.ampproject.org/v0/amp-mustache-0.1.js"
></script>
<script
  async
  custom-element="amp-form"
  src="https://cdn.ampproject.org/v0/amp-form-0.1.js"
></script>
<script
  async
  custom-element="amp-selector"
  src="https://cdn.ampproject.org/v0/amp-selector-0.1.js"
></script>

AMP 元件提供額外的功能和 UI 元件,可為 AMP 頁面新增豐富的互動功能。入門程式碼使用下列 AMP 元件

  • amp-carousel:圖片輪播,可顯示產品的多個檢視畫面。
  • amp-mustache:範本系統,用於轉譯來自 amp-form 的伺服器回應。
  • amp-form:為 <form> 元素新增特殊功能,這些功能是 AMP 頁面所必需的。
  • amp-selector:提供語意化的方式,可選取一組元素中的一個或多個元素。可用做 amp-form 的輸入來源。

基本互動功能

入門程式碼提供一些基本互動功能

  • 圖片輪播 (amp-carousel) 顯示產品的多個檢視畫面。
  • 使用者可以透過輕觸頁面底部的「加入購物車」按鈕,將產品加入購物車 (透過 amp-form)。

親自試試看:滑動圖片輪播並輕觸「加入購物車」按鈕。