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


Google AMP amp-image-lightbox用法及代碼示例


圖像庫是網站上流行的函數,借助amp-image-lightbox,開發人員可以輕鬆地將燈箱效果添加到AMP HTML頁麵,以激活amp-image-lightbox,我們可以對amp-image組件使用on動作。

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

HTML

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

屬性:



  • 布局:amp-image-lightbox僅支持無顯示布局。
  • ID:指定燈箱組件的ID。它通過on動作用作圖像的目標。
  • data-close-button-aria-label:用於為關閉按鈕設置aria-label。

例:

HTML

<!doctype html> 
<html amp> 
  
<head> 
    <meta charset="utf-8"> 
    <title>Google AMP amp-image-lightbox</title> 
  
    <script async src= 
        "https://cdn.ampproject.org/v0.js"> 
    </script> 
      
    <!-- Import the amp-image-lightbox  
        component in the header -->
    <script async custom-element="amp-image-lightbox"
src="https://cdn.ampproject.org/v0/amp-image-lightbox-0.1.js"> 
    </script> 
  
    <meta name="viewport" content= 
"width=device-width,minimum-scale=1,initial-scale=1"> 
  
    <link rel="canonical" href= 
"https://amp.dev/documentation/examples/components/amp-image-lightbox/index.html"> 
  
    <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> 
</head> 
  
<body> 
    <!-- The `amp-image-lightbox` is activated  
        using the `on` action on an `amp-img`  
        element-->
    <div> 
        <amp-img on="tap:lightbox1" role="button"
            tabindex="0" src= 
"https://pbs.twimg.com/profile_images/1304985167476523008/QNHrwL2q_400x400.jpg"
            layout="responsive"
            width="300" height="246"> 
        </amp-img> 
  
        <amp-image-lightbox id="lightbox1" 
            layout="nodisplay"> 
        </amp-image-lightbox> 
    </div> 
  
    <!--It is even possible to show different  
        images in the same amp-image-lightbox-->
    <amp-img on="tap:lightbox1" role="button"
        tabindex="0" src= 
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20200817185016/gfg_complete_logo_2x-min.png"
        layout="responsive" width="600" 
        height="400"> 
    </amp-img> 
  
    <!--The amp-image-lightbox also can  
        optionally display a caption for  
        the image at the bottom of the  
        viewport. -->
    <figure> 
        <amp-img on="tap:lightbox1" role="button"
            tabindex="0" src= 
"https://pbs.twimg.com/profile_images/1304985167476523008/QNHrwL2q_400x400.jpg"
            layout="responsive"
            width="300" height="246"> 
        </amp-img> 
          
        <figcaption>geeksforgeeks</figcaption> 
    </figure> 
</body> 
  
</html>

輸出:




相關用法


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