amp-iframe
Introduction
Use amp-iframe
for embedding content into AMP files via iframe. Useful for displaying content otherwise not (yet) supported by AMP.
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 theallow-same-origin
sandbox attribute. -
The
amp-iframe
must be defined with theresizable
attribute. -
The
amp-iframe
must have anoverflow
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:
-
Resize on load: the iframe size is initially set to
150x150
px via theamp-iframe
layout. On page load, the embedded iframe will resize itself to200x200
px. -
Resize on user interaction: pressing the button will resize the iframe to
300x300
px.
<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>
Se as explicações nesta página não respondem a todas as suas perguntas, entre em contato com outros usuários de AMP para discutir seu caso de uso específico.
Ir para o Stack Overflow Falta explicar algum recurso?O projeto AMP incentiva fortemente sua participação e contribuições! Esperamos que você se torne um participante assíduo de nossa comunidade de código aberto, mas também agradecemos contribuições pontuais para problemas que você tenha particular interesse.
Editar amostra no GitHub-
Written by @sebastianbenz