<amp-anim> - GIF画像を表示する
公開日:
AMPのamp-animは、GIF画像を表示するための要素です。基本的な仕様はamp-img要素と同じです。
概要
- 名前
- amp-anim
- 可用性
- Stable (安定)
- サポートするレイアウト
- fill,fixed,fixed-height,flex-item,nodisplay,responsive
- 必要なスクリプト
<script async custom-element="amp-anim" src="https://cdn.ampproject.org/v0/amp-anim-0.1.js"></script>
- 検証
- https://github.com/ampproject/amphtml/blob/master/extensions/amp-anim/0.1/validator-amp-anim.protoascii
- ドキュメント
- https://www.ampproject.org/docs/reference/extended/amp-anim.html
属性
alt
指定例: 芝犬の逆立ち
画像ファイルを表示できなかったり、見えなかったりする場合の代替テキスト。
attribution
指定例: CC BY
画像の属性。ライセンスの内容などを指定する。
height
指定例: 150
高さ。画像が表示される前にアスペクト比を決定できるよう、指定する必要がある。
src
指定例: ./image.png
画像ファイルのURL。公的にキャッシュできるURLでなければならない。リクエストによってはAMP側がURLを書き換えることもある。
srcset
指定例: /small.jpg 480w,/large.jpg 1130w
HTMLと同じsrcset属性の記法で、デバイスの解像度(画面幅)によって、違う画像を表示できる。
width
指定例: 200
横幅。画像が表示される前にアスペクト比を決定できるよう、指定する必要がある。
共通属性
AMPの全てのタグで利用できる共通属性を指定できます。
説明
ライブラリの読み込み
amp-animを利用するには、専用のライブラリを読み込む必要があります。
HTML
<script async custom-element="amp-anim" src="https://cdn.ampproject.org/v0/amp-anim-0.1.js"></script>
プレースホルダー
placeholder要素を付けた子要素を持つと、それが、画像が表示されるまでのプレースホルダーになります。テキストでも画像でもかまいません。
HTML
<amp-anim
src="/image/mascot.gif"
width="200"
height="150">
<p placeholder>GIF画像を準備中…。</p>
</amp-anim>
フォールバック
fallback要素を付けた子要素を持つと、それが、画像が表示できない場合のフォールバックになります。テキストでも画像でもかまいません。
HTML
<amp-anim
src="/image/mascot.gif"
width="200"
height="150">
<p fallback>GIF画像を表示できませんでした…。</p>
</amp-anim>
サンプルコード
HTML
<!doctype html>
<html amp>
<head>
<meta charset="utf-8">
<title>【デモ】<amp-anim> - GIF画像を表示する</title>
<link rel="canonical" href="https://syncer.jp/Web/AMP/Component/amp-anim/">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-custom>
body {
padding: 12px ;
background-color: #fff ;
}
</style>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "NewsArticle",
"headline": "記事のタイトル",
"image": [
"eyecatch.jpg"
],
"datePublished": "2017-08-23T00:00:00+0900"
}
</script>
<script async custom-element="amp-anim" src="https://cdn.ampproject.org/v0/amp-anim-0.1.js"></script>
<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>
<amp-anim
src="/image/mascot.gif"
width="200"
height="150">
</amp-anim>
</body>
</html>