on:tap 在 AMP 中添加 iframe

on:tap adding a iframe in AMP

我创建了一个 AMP 页面,我必须在其中添加一个要显示 PDF 的 iframe,

里面有两个问题:-

  1. 如何使用 on:tap 事件在 amp 中添加 iFrame(如果用户单击按钮,则应添加 iframe)

  2. 是否允许在 amp 页面中添加非 AMP 页面作为 iframe。

  1. How to add iFrame in amp with on:tap event (If a user click on a button the iframe should be add)

尝试这样的事情:

<!DOCTYPE html>
<html ⚡>

<head>
  <meta charset="utf-8" />
  <title>My AMP Page</title>
  <link rel="canonical" href="self.html" />
  <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1" />
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <script async custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"></script>
  <script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
  <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>
    .wrapper {
      max-width: 700px;
      margin: 0 auto;
    }
  </style>
</head>

<body>
  <main class="wrapper">
    <h1>My AMP Tap iframe</h1>
    <button type="button" on="tap:AMP.setState({ myIframeUrl: 'https://docs.google.com/gview?url=http%3A%2F%2Fwww.pdf995.com%2Fsamples%2Fpdf.pdf&embedded=true' })" [hidden]="myIframeUrl">Load my PDF iframe</button>
    <amp-iframe sandbox="allow-scripts allow-same-origin" src="https://google.com/" width="1" height="1" layout="responsive" [src]="myIframeUrl" [hidden]="!myIframeUrl" hidden>
      <div placeholder>PDF coming</div>
    </amp-iframe>
  </main>
</body>

</html>

现场演示: https://codepen.io/alexandr-kazakov/pen/wvzjqbj


  1. Is it Allowed to add non-AMP page as iframe in an amp page.

当然,例如,我们可以将 google 地图或 YouTube 播放器插入到 amp-iframe 中,尽管它们与 AMP 无关。