熟悉入门代码
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
)将产品添加到用户的购物车中。
试用一下:滑动图像轮播并点击“添加到购物车”按钮。