當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Google AMP amp-bind用法及代碼示例

有時您想向AMP頁麵添加自定義交互性,以使頁麵看起來更像user-friendly和用戶調用。盡管AMP的預建組件受到限製,但製造amp-bind可以解決此問題。它可以幫助開發人員在不使用AMP預製組件的情況下向頁麵添加自定義交互性。

設置:要在頁麵中使用amp-bind,必須將其腳本導入文檔標題中。

HTML

<script async custom-element="amp-bind"
    src="https://cdn.ampproject.org/v0/amp-bind-0.1.js">
</script>

Google AMP的amp-bind包含三個主要概念:



  1. State:狀態變量負責根據用戶操作在頁麵上進行更新。定義狀態變量非常重要。
  2. Expression:它們就像用於引用狀態的JavaScript表達式一樣。
  3. Binding:它們是一種特殊的屬性,用於通過表達式將元素的屬性鏈接到狀態。

例:單擊按鈕時動態添加文本。

HTML

<!doctype html>
<html amp>
 
<head>
    <meta charset="utf-8">
    <title>Google AMP amp-bind</title>
    <link rel="canonical" href=
"https://amp.dev/documentation/examples/components/amp-bind/index.html">
 
    <meta name="viewport" content=
"width=device-width,minimum-scale=1,initial-scale=1">
 
    <script async src=
        "https://cdn.ampproject.org/v0.js">
    </script>
 
    <script async custom-element="amp-bind"
src="https://cdn.ampproject.org/v0/amp-bind-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>
     
    <style amp-custom>
        h1 {
            color:forestgreen;
            text-align:center;
        }
    </style>
</head>
 
<body>
    <h1>
        Geeks For Geeks
    </h1>
 
    <div style="padding:1em;">
        <div hidden [hidden]="hideGreeting" 
            style="color:crimson;">
            Welcome to GeeksForGeeks
        </div>
 
        <button on=
"tap:AMP.setState({ hideGreeting:false })">
            Click Me!!
        </button>
    </div>
</body>
 
</html>

輸出:

說明:單擊該按鈕後,名為的隱藏屬性hideGreeting已更新,因為其狀態通過AMP.setState()操作進行了更新。

用途:amp-bind可用於多種用途,其中一些是:

  • 它可用於動態更改內容。
  • 它可用於動態更新映像。
  • 它可用於動態更改元素的類。
  • 它可用於更改CSS屬性的元素值。



相關用法


注:本文由純淨天空篩選整理自aditya_taparia大神的英文原創作品 Google AMP amp-bind。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。