AMP
  • websites

Video rotate to fullscreen with hint

Introduction

This is a sample template for implementing rotate-to-fullscreen with a user hint in AMP.

Setup

All scripts for our AMP components must be imported in the header. We will be using two components:

  • amp-video to render the video itself.
<script async custom-element="amp-video" src="https://cdn.ampproject.org/v0/amp-video-0.1.js"></script>
  • amp-animation to describe the animation to be triggered.
<script async custom-element="amp-animation" src="https://cdn.ampproject.org/v0/amp-animation-0.1.js"></script>

Style

Set some styling to position the hint and define how it looks.

<style amp-custom>
.video-container {
  height: 100%;
  width: 100vw;
  max-width: 700px;
  /* Absolute-positioned children will be positioned relative to this container: */
  position: relative;
  /* Cut off all elements based on container's rectangle: */
  overflow: hidden;
}
#video-hint {
  /* Position relative to its container: */
  position: absolute;
  right: 0;
  bottom: 25%;
  /* Typography: */
  line-height: 1;
  font-size: 13px;
  /* Borders, colors and sizing */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
  border-radius: 16px 0 0 16px;
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 10px 16px;
  /* Position on top of the video: */
  z-index: 100;
  /* Translate-x by 100% to displace out of view: */
  transform: translateX(100%);
  pointer-events: none;
  display: none;
}
/* Display hint only on touch devices */
.amp-mode-touch #video-hint {
  display: block;
}
</style>

Add the video and hint

Set rotate-to-fullscreen on the video element to actually enter fullscreen when the user rotates their device into landscape mode.

amp-video will trigger the animation as described in its on attribute when the firstPlay event occurs. This event is fired as soon as the user starts playing the video. In the case of an autoplay video like this one, the event will be fired as soon as the user taps on the video.

Rotate for fullscreen

Your browser doesn't support HTML5 video.

<div class="video-container">
  <div id="video-hint">Rotate for fullscreen</div>
  <amp-video width="480" height="270" src="/static/samples/video/tokyo.mp4" poster="/static/samples/img/tokyo.jpg" layout="responsive" controls on="firstPlay:hint-animation.start" rotate-to-fullscreen autoplay>
    <div fallback>
      <p>Your browser doesn't support HTML5 video.</p>
    </div>
    <source type="video/webm" src="/static/samples/video/tokyo.webm">
  </amp-video>
</div>

Define the animation

Describe an animation that will slide the user hint into view, and then slide it out.

It's important that the animation element has the hint-animation id, as that's the name which the video component uses to start the animation.

<amp-animation layout="nodisplay" id="hint-animation">
  <script type="application/json">
  {
    "selector": "#video-hint",
    "duration": "4.5s",
    "delay": "300ms",
    "fill": "both",
    "keyframes": [
      {"offset": 0, "transform": "translateX(100%)"},
      {"offset": 0.15, "transform": "translateX(0)"},
      {"offset": 0.8, "transform": "translateX(0)"},
      {"offset": 1, "transform": "translateX(100%)"}
    ]
  }
  </script>
</amp-animation>
자세한 설명이 필요하신가요?

이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.

Stack Overflow로 이동
설명이 부족한 기능을 발견하셨나요?

AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.

GitHub에서 샘플 수정하기