amp-font用于检查字体是否已在AMP HTML页面中加载。
所需脚本:将amp-font导入标头。
HTML
<script async custom-element="amp-font"
src="https://cdn.ampproject.org/v0/amp-font-0.1.js">
</script>
如果字体不可用,我们使用以下样式将字体显示为红色。
HTML
<style amp-custom>
:root {
--color-error:#B00020;
--color-primary:#005AF0;
}
@font-face {
font-family:'UnavailableFont';
font-style:normal;
font-weight:400;
src:url(fonts/UnavailableFont.ttf) format('truetype');
}
@font-face {
font-family:'Comic AMP';
font-style:normal;
font-weight:400;
src:local('Comic AMP'),
url(/static/samples/fonts/ComicAMP.ttf)
format('truetype');
}
@font-face {
font-family:'Comic AMP 2';
font-style:normal;
font-weight:400;
src:local('Comic AMP'),
url(/static/samples/fonts/ComicAMP2.ttf)
format('truetype');
}
.unavailable-font-loaded .unavailable-font {
font-family:'UnavailableFont';
}
.comicamp-loaded .comicamp {
font-family:'Comic AMP';
}
.comicamp2-loaded .comicamp2 {
font-family:'Comic AMP 2';
}
.comicamp-loading .comicamp,
.comicamp2-loading .comicamp2,
.unavailable-font-loading .unavailable-font {
color:var(--color-primary);
}
.comicamp-missing .comicamp,
.comicamp2-missing .comicamp2,
.unavailable-font-missing .unavailable-font {
color:var(--color-error);
}
</style>
属性:
- font-family:指定家族示例的字体:futura,georgia等。
- layout:定义此属性必须设置为nodisplay的特定布局。
例:在这里,第二个字体系列被设置为abc,因为没有名称为abc的系列,它以红色显示。
HTML
<!doctype html>
<html amp>
<head>
<meta charset="utf-8">
<title>Google AMP amp-font</title>
<script async src=
"https://cdn.ampproject.org/v0.js">
</script>
<script async custom-element="amp-font"
src="https://cdn.ampproject.org/v0/amp-font-0.1.js">
</script>
<link rel="canonical" href=
"https://amp.dev/documentation/examples/components/amp-font/index.html">
<meta name="viewport" content=
"width=device-width,minimum-scale=1,initial-scale=1">
<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>
:root {
--color-error:#B00020;
--color-primary:#005AF0;
}
@font-face {
font-family:'UnavailableFont';
font-style:normal;
font-weight:400;
src:url(fonts/UnavailableFont.ttf)
format('truetype');
}
@font-face {
font-family:'Comic AMP';
font-style:normal;
font-weight:400;
src:local('Comic AMP'),
url(/static/samples/fonts/ComicAMP.ttf)
format('truetype');
}
@font-face {
font-family:'Comic AMP 2';
font-style:normal;
font-weight:400;
src:local('Comic AMP'),
url(/static/samples/fonts/ComicAMP2.ttf)
format('truetype');
}
.unavailable-font-loaded .unavailable-font {
font-family:'UnavailableFont';
}
.comicamp-loaded .comicamp {
font-family:'Comic AMP';
}
.comicamp2-loaded .comicamp2 {
font-family:'Comic AMP 2';
}
.comicamp-loading .comicamp,
.comicamp2-loading .comicamp2,
.unavailable-font-loading .unavailable-font {
color:var(--color-primary);
}
.comicamp-missing .comicamp,
.comicamp2-missing .comicamp2,
.unavailable-font-missing .unavailable-font {
color:var(--color-error);
}
</style>
</head>
<body>
<!-- ## Existing font -->
<div>
<amp-font layout="nodisplay"
font-family="georgia" timeout="2000"
on-error-remove-class="comicamp-loading"
on-error-add-class="comicamp-missing"
on-load-remove-class="comicamp-loading"
on-load-add-class="comicamp-loaded">
</amp-font>
<p class="comicamp">
geeks for geeks
</p>
</div>
<!-- ## Unavailable font -->
<div>
<amp-font layout="nodisplay" font-family="abc"
timeout="2000" on-error-remove-class=
"unavailable-font-loading"
on-error-add-class="unavailable-font-missing"
on-load-remove-class="unavailable-font-loading"
on-load-add-class="unavailable-font-loaded">
</amp-font>
<p class="unavailable-font">
geeks for geeks
</p>
</div>
</body>
</html>
输出:
相关用法
- Google AMP amp-ad用法及代码示例
- Google AMP amp-accordion用法及代码示例
- Google AMP amp-facebook-like用法及代码示例
- Google AMP amp-carousel用法及代码示例
- Google AMP amp-date-countdown用法及代码示例
- Google AMP amp-brightcove用法及代码示例
- Google AMP amp-bind-recaptcha用法及代码示例
- Google AMP amp-autocomplete用法及代码示例
- Google AMP amp-img用法及代码示例
- Google AMP amp-image-lightbox用法及代码示例
- Google AMP amp-lightbox-gallery用法及代码示例
- Google AMP amp-soundcloud用法及代码示例
- Google AMP amp-mustache用法及代码示例
注:本文由纯净天空筛选整理自somsagar2019大神的英文原创作品 Google AMP amp-font。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。