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>
이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.
Stack Overflow로 이동 설명이 부족한 기능을 발견하셨나요?AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.
GitHub에서 샘플 수정하기-
Written by @sebastianbenz