
- Google AMP 教程
- Google AMP - 首頁
- Google AMP - 概述
- Google AMP - 簡介
- Google AMP - 圖片
- Google AMP - 表單
- Google AMP - 內嵌框架 (iFrames)
- 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 - 使用者通知
Google `amp-user-notification` 用於向用戶顯示可關閉的對話方塊訊息。我們可以用它來通知使用者頁面上的 Cookie。
要使用 `amp-user-notification`,需要在頁面上新增以下指令碼:
<script async custom-element = "amp-user-notification" src = "https://cdn.ampproject.org/v0/amp-user-notification-0.1.js"> </script>
`amp-user-notification` 標籤格式:
<amp-user-notification id = "my-notification" layout = "nodisplay"> <div>Example of amp-user-notification. <button on = "tap:my-notification.dismiss">I accept </button> </div> </amp-user-notification>
讓我們透過一個工作示例來了解 `amp-user-notification`:
示例
<!doctype html> <html amp lang = "en"> <head> <meta charset = "utf-8"> <script async src = "https://cdn.ampproject.org/v0.js"></script> <title>Google AMP - Amp Selector</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-user-notification" src = "https://cdn.ampproject.org/v0/amp-user-notification-0.1.js"> </script> <style amp-custom> div { font-size: 15px; background-color : #ccc; padding: 10px 10px; border-radius: 2px; } button{ background-color: #ACAD5C; color: white; cursor: pointer; float: right; } </style> </head> <body> <h3>Google AMP - Amp User Notification</h3> <amp-user-notification id = "my-notification" layout = "nodisplay"> <div>Example of amp-user-notification. <button on = "tap:my-notification.dismiss">I accept </button> </div> </amp-user-notification> </body> </html>
輸出
上面給出的工作示例程式碼的輸出如下所示:

使用者單擊按鈕後,通知將被關閉。關閉後,即使重新載入頁面,也不會再次顯示該通知。
使用者通知的資料儲存在瀏覽器 localStorage 中。如果清除 localStorage 並重新整理頁面,則可以再次看到通知。您可以使用瀏覽器控制檯中的 `localStorage.clear()` 來嘗試。
使用 `dismiss` 動作,可以透過在按鈕上使用該動作來關閉通知,如下所示:
<button on = "tap:my-notification.dismiss"> I accept </button>
當用戶點選按鈕時,通知將被關閉。
廣告