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) 以最基本的 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)。

试一试:滑动图像轮播并点击“添加到购物车”按钮。