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-iframemust set theallow-same-originsandbox attribute. -
The
amp-iframemust be defined with theresizableattribute. -
The
amp-iframemust have anoverflowchild element. -
The iframe document must send an
embed-sizerequest:
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
150x150px via theamp-iframelayout. On page load, the embedded iframe will resize itself to200x200px. -
Resize on user interaction: pressing the button will resize the iframe to
300x300px.
<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>
Si les explications de cette page ne répondent pas à vos questions, n'hésitez pas à contacter d'autres utilisateurs d'AMP pour discuter de votre cas d'utilisation spécifique.
Se rendre sur Stack Overflow Une fonctionnalité n'a pas encore été expliquée ?Le projet AMP encourage fortement votre participation et vos contributions ! Nous espérons que vous deviendrez un membre régulier de notre communauté open source, mais nous serons également ravis de recevoir des contributions ponctuelles concernant les questions qui vous intéressent particulièrement.
Modifier l'exemple sur GitHub-
Written by @sebastianbenz