熟悉入門程式碼
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
:為 AMP 頁面必要的<form>
元素新增特殊功能。amp-selector
:提供一種語意化的方式來選擇一組元素中的一個或多個元素。可用作 amp-form 的輸入來源。
基本互動性
入門程式碼提供了一些基本互動性
- 圖片輪播 (
amp-carousel
) 顯示產品的多種視圖。 - 透過點擊頁面底部的「加入購物車」按鈕,產品可以被 (透過
amp-form
) 加入使用者的購物車。
試試看:滑動圖片輪播並點擊「加入購物車」按鈕。