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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。