
- Google AMP 教程
- Google AMP - 首頁
- Google AMP - 概述
- Google AMP - 簡介
- Google AMP - 圖片
- Google AMP - 表單
- Google AMP - 內嵌框架 (Iframe)
- Google AMP - 影片
- Google AMP - 按鈕
- Google AMP - Timeago (時間顯示)
- Google AMP - MathML (數學標記語言)
- Google AMP - 自動調整文字大小 (Fit Text)
- Google AMP - 倒計時
- Google AMP - 日期選擇器
- Google AMP - 故事 (Story)
- 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 - 佈局
- Google AMP - 廣告
- 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 - 動態 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 會處理兩個重要的類:
- amp-referrer-*
- amp-viewer
讓我們詳細討論每個類。
amp-referrer-*
這些類根據使用者的訪問來源設定。這意味著如果使用者來自 Google,則會設定與 Google 相關的 referrer 類。Twitter 和 Pinterest 也同樣適用。
這些類根據引薦來源的型別提供。
例如,如果使用者從 Google 搜尋引擎點選 AMP 頁面,則會新增以下類:
- amp-referrer-www-google-com
- amp-referrer-google-com
- amp-referrer-com
同樣,Twitter、Pinterest、LinkedIn 等也有相應的類。
amp-viewer
AMP 檢視器基本上會更改 AMP URL 以從 Google 快取獲取詳細資訊。如果您在 Google 搜尋中搜索某些內容,顯示的輪播將包含所有 AMP 頁面。
單擊這些頁面後,它們會重定向到以 Google URL 為字首的 URL。當用戶在 AMP 檢視器中檢視頁面並使用動態類時,將設定 amp-viewer 類。

單擊 AMP 頁面後,您在位址列中獲得的 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

示例
<!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-metadata.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> <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 :)</p> </div> <div> <p class = "if-not-viewer">Hey! You are not coming from amp viewer</p> <p class = "if-viewer">Hey! From amp viewer.</p> <div> </body> </html>
輸出

廣告