
- Google AMP 教程
- Google AMP - 首頁
- Google AMP - 概述
- Google AMP - 簡介
- Google AMP - 圖片
- Google AMP - 表單
- Google AMP - 內嵌框架
- Google AMP - 影片
- Google AMP - 按鈕
- Google AMP - Timeago
- Google AMP - Mathml
- Google AMP - 適應文字
- Google AMP - 日期倒計時
- Google AMP - 日期選擇器
- Google AMP - 故事
- 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 - 資料繫結
amp-bind 幫助透過資料繫結和類似 JS 的表示式,根據操作為 amp 元件和 html 標籤新增互動性。本章詳細討論資料繫結。
要使用 amp-bind,我們需要將以下指令碼新增到我們的頁面中:
<script async custom-element = "amp-bind" src = "https://cdn.ampproject.org/v0/amp-bind-0.1.js"> </script>
讓我們藉助一個工作示例來全面理解這一點,如下所示:
示例
<!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 Bind</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> <style amp-custom> button{ background-color: #ACAD5C; color: white; padding: 12px 20px; border: none; border-radius: 4px; } </style> </head> <body> <h3>Google AMP - Amp Bind</h3> <p [text] = "'Hello ' + world + '.'"> Click on the button to change the text </p> <button on = "tap:AMP.setState({world: 'This is amp-bind example'})"> Click Here </button> </body> </html>
輸出

單擊按鈕以檢視文字更改,如下所示:

因此,在上面顯示的示例中,我們使用了 amp-bind 來更改單擊按鈕時的文字。
Amp-bind 有三個元件:
狀態 - 最初狀態為空。單擊按鈕後,狀態會更改。例如,
<button on = "tap:AMP.setState({world: 'This is amp-bind example'})"> Click Here </button>
AMP.setState 方法用於更改狀態。變數 world 被賦值為 This is amp-bind example。變數 world 用於 html 標籤中:
<p [text] = "'Hello ' + world + '.'"> Click on the button to change the text </p>
單擊按鈕時,world 被賦予一個新值:This is amp-bind example。
我們還可以將 amp-state 與繫結一起使用,如下所示:
<amp-state id = "myState"> <script type = "application/json"> { "foo": "bar" } </script> </amp-state>
表示式將在繫結期間被賦值為 bmyState.foo。
表示式 - amp-bind 工作的表示式如下所示:
'Hello ' + world
world 被稱為狀態變數。
繫結 - 繫結應用於 [屬性] 形式的特殊屬性。例如:
<p [text] = "'Hello ' + world + '.'"> Click on the button to change the text </p>
在上面的示例中,[text] 具有用於繫結 p 標籤的表示式。
我們可以使用以下屬性進行繫結:
- [text]
- [class]
- [hidden]
- [width]
- [height]
繫結也可以在 amp 元件上執行,並且僅允許特定屬性。以下列表顯示了此類元件和屬性:
序號 | Amp 元件 | 屬性 & 描述 |
---|---|---|
1 | <amp-carousel type=slides> | [slide]*
使用此繫結行為更改幻燈片 |
2 | <amp-date-picker> | [min]
min -> 設定最早可選日期 [max]max -> 設定最晚可選日期 |
3 | <amp-iframe> | [src]
更改 iframe 的 src |
4 | <amp-img> | [alt] [attribution] [src] [srcset]
我們可以更改 alt、attribution、src 和 srcset。如果更改了 src,請更改 srcset,因為它用於快取 |
5 | <amp-lightbox> | [open]*
您可以透過繫結到 open 來顯示/隱藏燈箱 |
6 | <amp-list> | [src]
如果表示式是字串,則從字串 URL 獲取並呈現 JSON。如果表示式是物件或陣列,則呈現表示式資料。 |
7 | <amp-selector> | [selected]* [disabled]
更改當前選定的子元素,這些元素由其 option 屬性值標識。支援逗號分隔的值列表以進行多選 |
使用 Amp-State 進行繫結
我們可以使用 amp-state 定義所有我們想要在 html 元素或 amp 元件上使用的資料。
amp-state 中使用的資料必須採用 json 格式,如下所示:
<amp-state id = "myCarsList"> <script type = "application/json"> { "currentcar" : "bmw", "audi": { "imageUrl": "images/audi.jpg" }, "bmw": { "imageUrl": "images/bmw.jpg" } } </script> </amp-state>
因此,我們定義了鍵值對,其中包含汽車的名稱和用於汽車的影像。
文字上的 Amp-bind 和 Amp-Image
下面顯示了一個使用 amp-state 和 amp-bind 的工作示例:
<!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 Bind</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> <style amp-custom> button{ background-color: #ACAD5C; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; float: left; } </style> </head> <body> <h3>Google AMP - Amp Bind</h3> <amp-state id = "myCarsList"> <script type = "application/json"> { "currentcar" : "bmw", "audi": { "imageUrl": "images/audi.jpg", "style": "greenBackground" }, "bmw": { "imageUrl": "images/bmw.jpg", "style": "redBackground" } } </script> </amp-state> <amp-img width = "300" height = "200" src = "images/bmw.jpg" [src] = "myCarsList[currentcar].imageUrl"> </amp-img> <p [text] = "'This is a ' + currentcar + '.'"> This is a BMW. </p> <br/> <button on = "tap:AMP.setState({currentcar: 'audi'})"> Change Car </button> </body> </html>
輸出

單擊按鈕以檢視汽車影像和下面的文字更改。

影片和內嵌框架上的 Amp-bind
我們現在將看到一個工作示例,該示例將更改 amp-iframe 和 amp-video 的 src。
<!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 Bind</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-video" src = "https://cdn.ampproject.org/v0/amp-video-0.1.js"></script> <script async custom-element = "amp-iframe" src = "https://cdn.ampproject.org/v0/amp-iframe-0.1.js"></script> <style amp-custom> button{ background-color: #ACAD5C; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; float: left; } </style> </head> <body> <h3>Google AMP - Amp Bind</h3> <button on = "tap:AMP.setState({currentlist: 'list1'})"> Click Here </button> <br/> <br/> <amp-state id = "myList"> <script type = "application/json"> { "currentlist" : "", "list1": { "url": "video/m.mp4", "style": "greenBackground", "iframeurl":"https://maps.google.com/maps?q=hyderabad&t=&z=13&ie=UTF8&iwloc=&output=embed" } } </script> </amp-state> <h3>AMP - IFRAME</h3> <amp-iframe width = "600" title = "Google map" height = "400" layout = "responsive" sandbox = "allow-scripts allow-same-origin allow-popups" frameborder = "0" src = "https://maps.google.com/maps?q=telangana&t=&z=13&ie=UTF8&iwloc=&output=embed" [src] = "myList[currentlist].iframeurl"> <amp-img layout = "fill" src = "images/loading.jpg" placeholder > /amp-img> </amp-iframe> <h3>AMP - VIDEO</h3> <amp-video id = "amp-video" src = "video/samplevideo.mp4" layout="responsive" [src] = "myList[currentlist].url" width = "300" height = "170" autoplay controls> </amp-video> </body> </html>
請注意,這裡我們使用了帶有 iframesrc 和影片 src 的 amp-state。
<amp-state id = "myList"> <script type = "application/json"> { "currentlist" : "", "list1": { "url": "video/m.mp4", "style": "greenBackground", "iframeurl":" https://maps.google.com/maps?q=hyderabad&t=&z=13&ie=UTF8&iwloc=&output=embed" } } </script> </amp-state>
currentlist 設定為空,點選按鈕後,它被設定為 list1。currentlist 變數用於 iframe 和影片的 src,如下所示:
<amp-iframe width="600" title = "Google map" height = "400" layout = "responsive" sandbox = "allow-scripts allow-same-origin allow-popups" frameborder = "0" src = "https://maps.google.com/maps?q=telangana&t=&z=13&ie=UTF8&iwloc=&output=embed" [src] = "myList[currentlist].iframeurl"> <amp-img layout = "fill" src = "images/loading.jpg" placeholder> </amp-img> </amp-iframe> <amp-video id = "amp-video" src = "video/samplevideo.mp4" layout = "responsive" [src] = "myList[currentlist].url" width = "300" height = "170" autoplay controls> </amp-video>
輸出

單擊按鈕以檢視影片和 iframe 的 src 更改。

帶有 amp-lightbox 的 Amp-bind
現在,讓我們看看繫結和 amp-lightbox 結合使用時的工作原理。
示例
<!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 Bind</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-lightbox" src = "https://cdn.ampproject.org/v0/amp-lightbox-0.1.js"></script> <style amp-custom> button{ background-color: #ACAD5C; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; float: left; } .lightbox { background: rgba(211,211,211,0.8); width: 100%; height: 100%; position: absolute; display: flex; align-items: center; justify-content: center; } </style> </head> <body> <h3>Google AMP - Amp Bind</h3> <button on = "tap:AMP.setState({displaylightbox: true})"> Click Here </button> <br/> <br/> <h3>AMP - Lightbox</h3> <amp-lightbox id = "my-lightbox" [open] = "displaylightbox" layout = "nodisplay" close-button> <div class = "lightbox" on = "tap:AMP.setState({displaylightbox: false})"> <amp-img alt = "Beautiful Flower" src = "images/loreal.gif" width = "246" height = "205"> </amp-img> </div> </amp-lightbox> </body> </html>
要在 amp-lightbox 上使用繫結,我們已在 amp-lightbox 上使用了 [open],如下所示:
<amp-lightbox id = "my-lightbox" [open] = "displaylightbox" layout = "nodisplay" close-button> <div class = "lightbox" on="tap:AMP.setState({displaylightbox: false})"> <amp-img alt = "Beautiful Flower" src = "images/loreal.gif" width = "246" height = "205"> </amp-img> </div> </amp-lightbox>
[open] = “displaylightbox” 是一個變數狀態,在單擊按鈕和點選燈箱 div 時更改為 true/false:
<button on = "tap:AMP.setState({displaylightbox: true})"> Click Here </button> <div class = "lightbox" on = "tap:AMP.setState({displaylightbox: false})"> <amp-img alt = "Beautiful Flower" src = "images/loreal.gif" width = "246" height = "205"> </amp-img> </div>
輸出

輸入元素的 Amp 繫結
讓我們藉助一個工作示例來了解 amp 繫結到輸入元素的工作原理,如下所示:
<!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 Bind</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-lightbox" src = "https://cdn.ampproject.org/v0/amp-lightbox-0.1.js"> </script> <style amp-custom> button{ background-color: #ACAD5C; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; float: left; } .lightbox { background: rgba(211,211,211,0.8); width: 100%; height: 100%; position: absolute; display: flex; align-items: center; justify-content: center; } #txtname{ width: 100%; padding: 12px 20px; margin: 8px 0; display: inline-block; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } div { font-size:25px; } </style> </head> <body> <h3>Google AMP - Amp Bind</h3> <button on = "tap:AMP.setState({displaylightbox: true})"> Click Here </button> <br/> <br/> <h3> AMP - Input Element <h3> <input id = "txtname" placeholder = "Type here" on = "input-throttled:AMP.setState({name: event.value})"> <div [text] = "name"> </div> </body> </html>
輸出

在文字框中輸入的資料顯示在底部。可以透過在輸入事件上更改狀態變數 name 來實現,如下所示:
<input id = "txtname" placeholder = "Type here" on = "input-throttled:AMP.setState({name: event.value})"> <div [text] = "name"> </div>