- 谷歌AMP教程
- Google AMP - 主页
- Google AMP - 概述
- 谷歌 AMP - 简介
- Google AMP - 图片
- Google AMP - 表单
- Google AMP - Iframe
- Google AMP - 视频
- Google AMP - 按钮
- Google AMP - Timeago
- Google AMP - 数学
- Google AMP - 适合文本
- Google AMP - 日期倒计时
- Google AMP - 日期选择器
- Google AMP - 故事
- Google AMP - 选择器
- Google AMP - 链接
- Google AMP - 字体
- Google AMP - 列表
- Google AMP - 用户通知
- Google AMP - 下一页
- Google AMP - 属性
- 样式和自定义 CSS
- Google AMP - 动态 CSS 类
- Google AMP - 操作和事件
- Google AMP - 动画
- Google AMP - 数据绑定
- Google AMP - 布局
- 谷歌 AMP - ADS
- Google AMP - 分析
- Google AMP - 社交小部件
- Google AMP - 媒体
- Html 页面到 Amp 页面
- Google AMP - 基本语法
- Google AMP - 验证
- Google AMP - 缓存
- Google AMP - 自定义 Javascript
- Google AMP - Cors
- Google AMP 有用资源
- Google AMP - 快速指南
- Google AMP - 有用的资源
- Google AMP - 讨论
Google AMP - Html 页面到 Amp 页面
在本章中,我们将了解如何将普通 html 页面转换为 amp 页面。我们还将验证 amp 页面并检查最后的输出。
首先,让我们使用正常的 html 页面,如下所示 -
测试.html
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Tutorials</title>
<link href = "style.css" rel = "stylesheet" />
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
<script src = "js/jquery.js"></script>
</head>
<body>
<header role = "banner">
<h2>Tutorials</h2>
</header>
<h2>Some Important Tutorials List</h2>
<article>
<section>
<img src = "images/tut1.png" width="90%" height = "90%"/>
</section>
<section>
<img src = "images/tut2.png" width="90%" height = "90%"/>
</section>
<section>
<img src = "images/tut3.png" width="90%" height = "90%"/>
</section>
<section>
<img src = "images/tut4.png" width="90%" height = "90%"/>
</section>
</article>
<footer>
<p>For More tutorials Visit <a href =
"https://tutorialspoint.com/">Tutorials Point</a></p>
</footer>
</body>
</html>
请注意,我们在其中使用 style.css,css 文件的详细信息如下所示 -
h1 {color: blue;text-align: center;}
h2 {text-align: center;}
img {
border: 1px solid #ddd;
border-radius: 4px;
padding: 5px;
}
article {
text-align: center;
}
header{
width: 100%;
height: 50px;
margin: 5px auto;
border: 1px solid #000000;
text-align: center;
background-color: #ccc;
}
footer {
width: 100%;
height: 35px;
margin: 5px auto;
border: 1px solid #000000;
text-align: center;
background-color: yellow;
}
请注意,我们还在上面列出的 .html 中使用了 jquery.js 文件。
现在,在本地托管 test.html 并查看此处给出的链接中看到的输出 -
http://localhost:8080/googleamp/test.html
现在,让我们一步步将上面的 test.html 文件更改为 test_amp.html 文件。
首先,我们必须将 test.html 保存为 test_amp.html 并按照以下步骤操作。
步骤 1 - 在头部添加 amp 库,如下所示 -
<script async src = "https://cdn.ampproject.org/v0.js"> </script>
例如,一旦添加到 test_amp.html,它将如下所示 -
<head>
<meta charset = "utf-8">
<title>Tutorials</title>
<script async src = "https://cdn.ampproject.org/v0.js">
</script>
<link href = "style.css" rel = "stylesheet" />
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
<script src = "js/jquery.js"></script>
</head>
现在在浏览器中运行页面 test_amp.html 并打开浏览器控制台。它将显示控制台消息,如下所示 -
要知道您的 html 文件是否是有效的 amp,请将 #development=1 添加到您的 html 页面 URL 末尾,如下所示 -
http://localhost:8080/googleamp/test_amp.html#development=1
在浏览器和 Google Chrome 控制台中点击上述网址。它会列出放大器认为从放大器规格的角度来看无效的错误。
我们得到的 test_amp.html 错误如下所示 -
现在让我们一一修复它们,直到收到成功消息。
步骤 2 - 我们可以在控制台中看到以下错误 -
我们可以通过为 html 标签添加 ⚡ 或 amp 来解决这个问题。我们将 amp 添加到 html 标签,如下所示 -
<html amp>
步骤 3 - 请确保您的头标签中有带有字符集和 name=”viewport” 的元标签,如下所示 -
<head> <meta charset = "utf-8"> <meta name = "viewport" content = "width = device-width, initial-scale = 1.0"> </head>
步骤 4 - 我们遇到的下一个错误如下所示 -
它说链接 rel=stylesheet 中的 href 即以下链接抛出错误。这是因为 amp 不允许使用带有 href 的链接的外部样式表放入页面内。
<link href = "style.css" rel = "stylesheet" />
我们可以在 style.css 中添加所有 css,如下所示 -
<style amp-custom> /*All styles from style.css please add here */ </style>
因此,必须使用 amp-custom 属性将 style.css 中存在的 css 数据添加到 style 中。
<style amp-custom>
h1 {color: blue;text-align: center;}
h2 {text-align: center;}
img {
border: 1px solid #ddd;
border-radius: 4px;
padding: 5px;
}
article {
text-align: center;
}
header{
width: 100%;
height: 50px;
margin: 5px auto;
border: 1px solid #000000;
text-align: center;
background-color: #ccc;
}
footer {
width: 100%;
height: 35px;
margin: 5px auto;
border: 1px solid #000000;
text-align: center;
background-color: yellow;
}
</style>
将样式标签添加到您的放大器页面。现在让我们在浏览器中使用上面的样式标签进行相同的测试。到目前为止,我们对 test_amp.html 所做的更改如下所示 -
<!DOCTYPE html>
<html amp>
<head>
<meta charset = "utf-8">
<title>Tutorials</title>
<script async src = "https://cdn.ampproject.org/v0.js">
</script>
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
<script src = "js/jquery.js"></script>
<style amp-custom>
h1 {color: blue;text-align: center;}
h2 {text-align: center;}
img {
border: 1px solid #ddd;
border-radius: 4px;
padding: 5px;
}
article {
text-align: center;
}
header{
width: 100%;
height: 50px;
margin: 5px auto;
border: 1px solid #000000;
text-align: center;
background-color: #ccc;
}
footer {
width: 100%;
height: 35px;
margin: 5px auto;
border: 1px solid #000000;
text-align: center;
background-color: yellow;
}
</style>
</head>
<body>
<header role = "banner">
<h2>Tutorials</h2>
</header>
<h2>Some Important Tutorials List</h2>
<article>
<section>
<img src = "images/tut1.png" width = "90%" height = "90%"/>
</section>
<section>
<img src = "images/tut2.png" width = "90%" height = "90%"/>
</section>
<section>
<img src = "images/tut3.png" width = "90%" height = "90%"/>
</section>
<section>
<img src = "images/tut4.png" width="90%" height = "90%"/>
</section>
</article>
<footer>
<p>For More tutorials Visit <a href =
"https://tutorialspoint.com/">Tutorials Point</a></p>
</footer>
</body>
</html>
让我们看看上面页面控制台中的输出和错误。观察以下屏幕截图 -
控制台中显示的错误如下 -
现在,您可以看到对于 amp 的一些错误,样式已被删除。现在让我们修复剩余的错误。
步骤 5 - 我们在列表中看到的下一个错误如下 -
我们添加了调用 jquery 文件的脚本标签。请注意,amp 页面不允许在页面中使用任何自定义 JavaScript。我们必须将其删除并确保使用可用的放大器组件。
例如,如果需要任何动画,我们有 amp-animation,如果我们想向页面添加谷歌分析代码,我们有 amp-analytics。同样,我们有 amp-ad 组件来显示要在页面上显示的广告。还有一个 amp-iframe 组件,我们可以将 src 指向同一源,并在 amp-iframe 中需要时调用任何自定义 javascript。
现在,让我们从页面中删除脚本标签。
步骤 6 - 显示的下一个错误如下所示 -
上述错误指向我们在页面上使用的图像标签。Amp 不允许在页面内使用 <img src=”” /> 标签。请注意,我们需要使用 amp-img 标签。
让我们用 <amp-img> 替换 <img> 标签,如下所示 -
<section>
<amp-img alt = "Beautiful Flower"
src = "images/tut1.png"
width = "500"
height = "160"
layout = "responsive">
</amp-img>
</section>
<section>
<amp-img alt = "Beautiful Flower"
src = "images/tut2.png"
width = "500"
height = "160"
layout = "responsive">
</amp-img>
</section>
<section>
<amp-img alt = "Beautiful Flower"
src = "images/tut3.png"
width = "500"
height = "160"
layout = "responsive">
</amp-img>
</section>
<section>
<amp-img alt = "Beautiful Flower"
src = "images/tut4.png"
width = "500"
height = "160"
layout = "responsive">
</amp-img>
</section>
我们已将所有 <img> 标记替换为 <amp-img>,如上所示。现在,让我们在浏览器中运行该页面以查看输出和错误 -
错误
观察到错误现在越来越少了。
步骤 7 - 控制台中显示的下一个错误如下 -
我们需要在 head 部分添加链接 rel=canonical 标签。请注意,这是一个强制性标签,应始终添加在头部,如下所示 -
<link rel = "canonical" href = "http://example.ampproject.org/article-metadata.html">
步骤 8 - 由于控制台中缺少noscript 标记而显示下一个错误,如下所示 -
我们需要在头部添加用 amp-boilerplate 括起来的 <noscript> 标签,如下所示 -
<noscript>
<style amp-boilerplate>
body{
-webkit-animation:none;
-moz-animation:none;
-ms-animation:none;
animation:none}
</style>
</noscript>
步骤 9 - 显示的下一个错误如下 -
另一个强制标签是带有 amp-boilerplate 的 style 标签,必须放置在 noscript 标签之前。带有 amp-boilerplate 的样式标签如下所示 -
<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>
将上述样式标签添加到 test_amp.html 页面。
完成后在浏览器中测试页面以查看输出和控制台 -
控制台详细信息如下所示 -
因此,我们终于解决了所有错误,现在页面 test_amp.html 是一个有效的 amp 页面。
由于页眉和页脚被截断,需要添加一些样式,我们可以在已添加的自定义样式中更新相同的样式。因此我们从页眉和页脚中删除了 width:100%。
这是最终输出 -
最终的 test_amp.html 文件
<!DOCTYPE html>
<html amp>
<head>
<meta charset = "utf-8">
<title>Tutorials</title>
<link rel = "canonical" href=
"http://example.ampproject.org/article-metadata.html">
<script async src = "https://cdn.ampproject.org/v0.js">
</script>
<meta name = "viewport" content = "width = device-width,
initial-scale = 1.0">
<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>
<style amp-custom>
h1 {color: blue;text-align: center;}
h2 {text-align: center;}
amp-img {
border: 1px solid #ddd;
border-radius: 4px;
padding: 5px;
}
article {
text-align: center;
}
header{
height: 50px;
margin: 5px auto;
border: 1px solid #000000;
text-align: center;
background-color: #ccc;
}
footer {
height: 35px;
margin: 5px auto;
border: 1px solid #000000;
text-align: center;
background-color: yellow;
}
</style>
</head>
<body>
<header role = "banner">
<h2>Tutorials</h2>
</header>
<h2>Some Important Tutorials List</h2>
<article>
<section>
<amp-img
alt = "Beautiful Flower"
src = "images/tut1.png"
width = "500"
height = "160"
layout = "responsive">
</amp-img>
</section>
<section>
<amp-img
alt = "Beautiful Flower"
src = "images/tut2.png"
width = "500"
height = "160"
layout = "responsive">
</amp-img>
</section>
<section>
<amp-img
alt = "Beautiful Flower"
src = "images/tut3.png"
width = "500"
height = "160"
layout = "responsive">
</amp-img>
</section>
<section>
<amp-img
alt = "Beautiful Flower"
src = "images/tut4.png"
width = "500"
height = "160"
layout = "responsive">
</amp-img>
</section>
</article>
<footer>
<p>For More tutorials Visit <a href =
"https://tutorialspoint.com/">
Tutorials Point</a>
</p>
</footer>
</body>
</html>
这样,最后我们就完成了将普通 html 文件转换为 amp 的工作。