Google-amp-dynamic-css-classes

提供:Dev Guides
移動先:案内検索

Google AMP-動的CSSクラス

amp-dynamic-css-classesは、動的クラスをbodyタグに追加します。 この章では、このタグの詳細を学びましょう。

amp-dynamic-css-classesを使用するには、次のスクリプトを追加する必要があります-

<script asynccustom-element="amp-dynamic-css-classes"
   src = "https://cdn.ampproject.org/v0/amp-dynamic-css-classes-0.1.js">
</script>

amp-dynamic-css-classesによって世話をされる2つの重要なクラスがあります-

*amp-referrer-*
*アンプビューア

それぞれについて詳しく説明します。

amp-referrer-*

これらのクラスは、ユーザーの来方に応じて設定されます。 ユーザーがGoogleから来ている場合、Googleに関連するリファラークラスが設定されます。 同じことがTwitterとPinterestにも当てはまります。

クラスはリファラーのタイプに基づいて利用可能です。

たとえば、Googleの場合、ユーザーがGoogle検索エンジンからampページをクリックすると、次のクラスが追加されます。

  • amp-referrer-www-google-com
  • amp-referrer-google-com
  • amp-referrer-com

同様に、Twitter、Pinterest、Linkedinなどで利用できるクラスがあります。

アンプビューア

Ampビューアーは、基本的にアンプのURLを変更して、Googleキャッシュから詳細を取得します。 Google検索で何かを検索すると、表示されるカルーセルにはすべてのアンプページが表示されます。

それらをクリックすると、接頭辞としてGoogle urlを使用してURLにリダイレクトされます。 amp-viewerクラスは、ユーザーがamp-viewerでページを表示し、動的クラスを使用しているときに設定されます。

amp viewer

アンペアページをクリックすると、アドレスバーに表示されるURLは次のようになります-

https://www.google.co.in/amp/s/m.timesofindia.com/sports/cricket/india-in-australia/to-hell-with-the-nets-boys-need-rest-ravi-shastri/amp_articleshow/67022458.cms

amp viewer

<!doctype html>
<html amp lang = "en">
   <head>
      <meta charset =  "utf-8">
      <script async src = "https://cdn.ampproject.org/v0.js"></script>
      <title>Google AMP - Dynamic Css Classes</title>
      <link rel = "canonical" href = "http://example.ampproject.org/article-metadatal">
      <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>

      <script async custom-element = "amp-bind"
         src = "https://cdn.ampproject.org/v0/amp-bind-0.1.js">
      </script>
      <script async custom-element = "amp-dynamic-css-classes"
         src = "https://cdn.ampproject.org/v0/amp-dynamic-css-classes-0.1.js">
      </script>
      <link rel = "stylesheet" href = "https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css">

      <style amp-custom>
         body:not(.amp-referrer-pinterest-com) .if-pinterest,
         body:not(.amp-referrer-ampbyexample-com) .if-ampbyexample,
         body:not(.amp-referrer-google-com) .if-google,
         body:not(.amp-referrer-twitter-com) .if-twitter,
         body:not(.amp-referrer-linkedin-com) .if-linkedin,
         body:not(.amp-referrer-localhost) .if-localhost {
            display: none;
         }
         body:not(.amp-viewer) .if-viewer,
            body.amp-viewer .if-not-viewer {
            display: none;
         }
         p {
            padding: 1rem;
            font-size:25px;
         }
      </style>
   </head>
   <body>
      <h3>Google AMP - Dynamic Css Classes</h3>
      <div>

         <p class = "if-pinterest">You were referred here or embedded by Pinterest!</p>
         <p class = "if-twitter">You were referred here or embedded by Twitter!</p>
         <p class = "if-google">You were referred here or embedded by Google!</p>
         <p class = "if-ampbyexample">You came here directly! Cool :)</p>
         < class = "if-localhost">You came here directly! Cool :)&lt/p>
      &lt/div>
      &ltdiv>
         <p class = "if-not-viewer">Hey! You are not coming from amp viewer&lt/p>
         <p class = "if-viewer">Hey! From amp viewer.</p>
      <div>
   </body>
</html>

出力

amp動的cssクラス