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


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

邊欄是Web頁麵中非常常見的函數,非常簡單,但是用HTML CSS編寫代碼卻很長。在AMP HTML中,我們使用amp-sidebar組件在頁麵上添加側邊欄。側邊欄提供對元內容的訪問權限可以通過單擊按鈕來顯示。

必需的腳本:將amp-sidebar組件導入標頭。

HTML

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

屬性:



  • side:指定側邊欄應從左側或右側彈出頁麵的哪一側。保留其默認值。
  • layout:指定側欄的布局,始終設置為nodisplay。
  • toolbar:此屬性用於子<nav工具欄=“”>上,並接受媒體查詢以在邊欄中顯示。
  • data-close-button-aria-label:指定按鈕是否為aria-label。

例:

HTML

<!doctype html>
<html ⚡>
 
<head>
    <meta charset="utf-8">
    <title>Google AMP amp-sidebar</title>
     
    <script async src=
        "https://cdn.ampproject.org/v0.js">
    </script>
     
    <!--Import the `amp-sidebar` component.-->
    <script async custom-element="amp-sidebar"
src="https://cdn.ampproject.org/v0/amp-sidebar-0.1.js">
    </script>
     
    <script async custom-element="amp-fit-text"
src="https://cdn.ampproject.org/v0/amp-fit-text-0.1.js">
    </script>
     
    <link rel="canonical" href=
"https://amp.dev/documentation/examples/components/amp-sidebar/index.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>
 
    <style amp-custom>
        .btn {
            margin-top:50px;
            margin-left:50px
        }
    </style>
</head>
 
<body>
    <amp-sidebar id="sidebar"
        class="sample-sidebar"
        layout="nodisplay" side="right">
         
        <h3>Sidebar</h3>
        <button on="tap:sidebar.close"
            class="btn">Close sidebar
        </button>
         
        <button on="tap:sidebar.toggle"
            class="btn">Toggle sidebar
        </button>
    </amp-sidebar>
 
    <button on="tap:sidebar.toggle"
        class="btn">Toggle sidebar
    </button>
     
    <button on="tap:sidebar.open"
        class="btn">Open sidebar
    </button>
</body>
 
</html>

輸出:

主頁

側杆




相關用法


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