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


Google AMP amp-carousel用法及代码示例


amp-carousel组件用于在AMP HTML中沿水平轴制作图像/内容轮播。轮播是一系列图像的幻灯片显示。

必需的脚本:在标题中导入amp-carousel组件。

HTML

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

属性:



  1. type:它指定轮播的类型。默认情况下,类型设置为轮播(所有幻灯片均水平滚动,此处的幻灯片可能具有不同的CSS)。也可以将其设置为幻灯片(一次显示单个幻灯片)。
  2. control:它显示用于用户导航的左右箭头。
  3. autoplay:当我们使用此属性时,它会自动移动轮播。
  4. loop:使用循环属性进行循环,即在最后一张幻灯片中有右控件,在第一张幻灯片中有左控件。
  5. delay:默认情况下,下一张幻灯片的显示持续时间设置为5000ms,并且延迟不能小于1000ms。
  6. slide:它用于指定应该先放哪个幻灯片。

范例1:使用type =“ slides”将图像列表显示为幻灯片。

HTML

<!doctype html> 
<html amp> 
  
<head> 
    <meta charset="utf-8"> 
    <title>amp-carousel</title> 
    <script async src= 
        "https://cdn.ampproject.org/v0.js"> 
    </script> 
      
    <!-- Import the carousel component in the header. -->
    <script async custom-element="amp-carousel"
        src= 
"https://cdn.ampproject.org/v0/amp-carousel-0.1.js"> 
    </script> 
  
    <link rel="canonical" href= 
"https://amp.dev/documentation/examples/components/amp-carousel/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> 
</head> 
  
<body> 
    <amp-carousel width="400" height="300" 
            layout="responsive" type="slides"> 
        <amp-img src= 
"https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-9.png"
            width="400" height="300"
            layout="responsive"> 
        </amp-img> 
  
        <amp-img src= 
"https://media.geeksforgeeks.org/wp-content/uploads/20190918234528/colorize1.png"
            width="400" height="300"
            layout="responsive"> 
        </amp-img> 
  
        <amp-img src= 
"https://media.geeksforgeeks.org/wp-content/uploads/20190918234815/colorize2.png"
            width="400" height="300"
            layout="responsive"> 
        </amp-img> 
    </amp-carousel> 
</body> 
  
</html>

输出:

范例2:使用具有3000ms延迟的自动播放属性。

HTML

<!doctype html> 
<html amp> 
  
<head> 
    <meta charset="utf-8"> 
    <title>amp-carousel</title> 
    <script async src= 
        "https://cdn.ampproject.org/v0.js"> 
    </script> 
      
    <!-- Import the carousel component  
        in the header. -->
    <script async custom-element="amp-carousel"
        src= 
"https://cdn.ampproject.org/v0/amp-carousel-0.1.js"> 
    </script> 
  
    <link rel="canonical" href= 
"https://amp.dev/documentation/examples/components/amp-carousel/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> 
</head> 
  
<body> 
    <amp-carousel width="400" height="300"
            layout="responsive" type="slides"
            autoplay delay="3000"> 
        <amp-img src= 
"https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-9.png"
            width="400" height="300"
            layout="responsive"> 
        </amp-img> 
  
        <amp-img src= 
"https://media.geeksforgeeks.org/wp-content/uploads/20190918234528/colorize1.png"
            width="400" height="300"
            layout="responsive"> 
        </amp-img> 
  
        <amp-img src= 
"https://media.geeksforgeeks.org/wp-content/uploads/20190918234815/colorize2.png"
            width="400" height="300"
            layout="responsive"> 
        </amp-img> 
    </amp-carousel> 
</body> 
  
</html>

输出:




相关用法


注:本文由纯净天空筛选整理自somsagar2019大神的英文原创作品 Google AMP amp-carousel。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。