AMP
  • websites

amp-iframe

Introduction

Use amp-iframe for embedding content into AMP files via iframe. Useful for displaying content otherwise not (yet) supported by AMP.

We're using Vimeo embeds here for an example, but any kind of content works. If you're interested in embedding Vimeo in AMP, it's recommended that you use the provided amp-vimeo component instead.

Import the amp-iframe component in the header.

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

You can use the amp-bind extension to dynamically change an iframe src.

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

First viewport

Iframes must be either 600px away from the top or not within the first 75% of the viewport when scrolled to the top - whichever is smaller. This example might not work depending on your screen width. In that case it will only show a loading indicator.

<amp-iframe width="500" height="281" title="Netflix House of Cards branding: The Stack" layout="responsive" sandbox="allow-scripts allow-same-origin allow-popups" allowfullscreen frameborder="0" src="https://player.vimeo.com/video/140261016">
</amp-iframe>

Placeholder

The 600px away from the top constraint can be avoided by adding a placeholder image.

<amp-iframe width="500" height="281" title="Netflix House of Cards branding: The Stack" layout="responsive" sandbox="allow-scripts allow-same-origin allow-popups" allowfullscreen frameborder="0" src="https://player.vimeo.com/video/140261016">
  <amp-img layout="fill" src="https://i.vimeocdn.com/video/536538454_640.webp" placeholder></amp-img>
</amp-iframe>

Giphy

Here is another iframe sample embedding an animated GIF from Giphy.

<amp-iframe width="600" height="400" title="Animated dancing GIF from Giphy" layout="responsive" sandbox="allow-scripts allow-same-origin allow-popups" frameborder="0" src="https://giphy.com/embed/DKG1OhBUmxL4Q">
</amp-iframe>

Google Maps

Embedding Google Maps is possible via the Google Maps Embed API and requires an API Key. Make sure your API key allows requests from the ampproject.org domain.

<amp-iframe width="600" height="400" title="Google map pin on Googleplex, Mountain View CA" layout="responsive" sandbox="allow-scripts allow-same-origin allow-popups" frameborder="0" src="https://www.google.com/maps/embed/v1/place?q=place_id:ChIJ2eUgeAK6j4ARbn5u_wAGqWA&key=AIzaSyAyAS599A2GGPKTmtNr9CptD61LE4gN6oQ">
</amp-iframe>

Resizable iframe

One important feature of amp-iframe is the ability to resize the iframe at runtime. For this to work:

  • The amp-iframe must set the allow-same-origin sandbox attribute.

  • The amp-iframe must be defined with the resizable attribute.

  • The amp-iframe must have an overflow child element.

  • The iframe document must send an embed-size request:

window.parent.postMessage({
  sentinel: 'amp',
  type: 'embed-size',
  height: document.body.scrollHeight
}, '*');

The following example demonstrates two resizing use cases:

  1. Resize on load: the iframe size is initially set to 150x150px via the amp-iframe layout. On page load, the embedded iframe will resize itself to 200x200px.

  2. Resize on user interaction: pressing the button will resize the iframe to 300x300px.

Click to show more
<amp-iframe id="sample-resizable-iframe" title="Resizable iframe example from 200x200 to 300x300 " width="150" height="150" sandbox="allow-scripts allow-same-origin" resizable frameborder="0" src="https://preview.amp.dev/static/samples/files/resizable-iframe.html">
            <div overflow tabindex="0" role="button" aria-label="Show more">Click to show more</div>
</amp-iframe>

Dynamically changing the iframe src

With amp-bind the src for iframes embedded with amp-iframe can be changed.

<amp-iframe id="amp-iframe" title="Cute dog video" width="500" height="281" layout="responsive" sandbox="allow-scripts allow-same-origin allow-popups" allowfullscreen frameborder="0" src="https://player.vimeo.com/video/183849543" [src]="myIframeUrl">
</amp-iframe>
<button on="tap:AMP.setState({ myIframeUrl: 'https://player.vimeo.com/video/185199565' })">Change src</button>
Perlu penjelasan lebih lanjut?

Jika penjelasan pada halaman ini tidak menjawab semua pertanyaan Anda, silakan hubungi pengguna AMP lainnya untuk mendiskusikan contoh penggunaan Anda yang spesifik.

Kunjungi Stack Overflow
Fitur yang tidak dijelaskan?

Proyek AMP sangat menganjurkan partisipasi dan kontribusi Anda! Kami berharap Anda akan terus ambil bagian dalam komunitas sumber terbuka kami, tetapi kami juga menerima kontribusi satu kali untuk topik tertentu yang Anda minati.

Edit sampel di GitHub