当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。