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


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

介绍:

我们使用此组件制作一个容器,以AMP HTML文件显示广告。

官方网站:https://amp.dev/

所需脚本:



在标题中导入amp-ad组件

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

属性:

  • Placeholder -amp-ad支持带有占位符属性的子元素。如果出现网络问题并且看不到广告,则会显示占位符
<amp-ad width="100" height="100"> 
  <div placeholder>No geeky ads to show</div> 
</amp-ad>
  • 倒退-amp-ad支持具有后备属性的子元素。如果该广告位没有可用的广告,则显示此元素
<amp-ad width="100" height="100"> 
  <div fallback>geeks for geeks has no ad available at the moment</div> 
</amp-ad>

例:

<!doctype html> 
<html ⚡> 
<head> 
<meta charset="utf-8"> 
  <title>amp-ad</title> 
  <script async src= 
          "https://cdn.ampproject.org/v0.js"> 
  </script> 
  <!-- Import the `amp-ad` component in the header. -->
  <script async custom-element="amp-ad" 
          src="https://cdn.ampproject.org/v0/amp-ad-0.1.js"> 
  </script> 
  <link rel="canonical" 
        href= 
"https://amp.dev/documentation/examples/components/amp-ad/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> 
<h3>geeksforgeeks amp | ad<h3> 
  <!-- 
    `amp-ad` requires `width` and  
   `height` values. Select an ad network 
     via the type argument. 
  -->
  <amp-ad width="300" 
          height="250" 
          type="a9"
          data-amzn_assoc_ad_mode="auto" 
          data-divid= 
 "amzn-assoc-ad-fe746097-f142-4f8d-8dfb-45ec747632e5" 
          data-recomtype="async" 
          data-adinstanceid= 
          "fe746097-f142-4f8d-8dfb-45ec747632e5"> 
  </amp-ad> 
  
  <!-- Placeholder-->
  <!-- 
    Optionally `amp-ad` supports a child  
element with the `placeholder` attribute. 
    If supported by the ad network,  
this element is shown until the ad is available 
    for viewing. 
  -->
  <amp-ad width="300"
          height="200"
          type="doubleclick"
          data-slot="/4119129/doesnt-exist"> 
    <div placeholder> 
      <b> gfg Placeholder here!!!</b> 
    </div> 
  </amp-ad> 
  
  <!--Fallback-->
  <!-- 
    `amp-ad` supports a child element  
with the `fallback` attribute. 
    If supported by the ad network, this  
element is shown if no ad is available  
    for this slot. 
  -->
  <amp-ad width="300"
          height="200" 
          type="doubleclick" 
          data-slot="/4119129/doesnt-exist"> 
    <div fallback> 
      <p>No ad</p> 
    </div> 
  </amp-ad> 
 </body> 
</html>

输出:




相关用法


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