Do you build things with AMP? Fill out the new AMP Developer Survey!
AMP
  • ads

Video Ad

Summary

Sample AMPHTML ad using amp-video, to display a video ad with layers and playback control.

Styling

This is an advanced example that requires some styling to make it look and function properly. The styling designed here is responsive and will work with various ad sizes.

<style amp-custom>
#splash-container, #video-container {
  /* Set to ad dimensions */
  width: 300px;
  height: 168px;

  /* Set so children can position themselves in relation to the container */
  position: relative;

  /* Basic font styles */
  font-family: sans-serif;
  line-height: 1;
  color: #fff;
}
.main-link {
  display: block;
  color: white;
}
.brand {
  /* Place in top-left corner */
  position: absolute;
  z-index: 1;
  top: 10px;
  left: 10px;
}
.watch-video {
  /* Position roughly in vertical center */
  position: absolute;
  z-index: 1;
  top: 70px;
  left: 10px;

  /* Button styles */
  padding: 7px;
  border: gray solid 1px;
  border-radius: 7px;
  background-color: rgba(0, 0, 0, 0.8);
  cursor: pointer;
}
.title {
  /* Place beneath "Watch Video" button */
  position: absolute;
  z-index: 1;
  top: 110px;
  left: 10px;
  text-shadow: 2px 2px 8px rgba(0, 0, 0, 0.8);

  /* Font styles */
  font-weight: bold;
  font-size: 18px;
}
.close {
  /* Place in top-right corner */
  position: absolute;
  z-index: 1;
  right: 10px;
  top: 10px;

  /* Button styles */
  cursor: pointer;
  font-weight: bold;
  font-size: 18px;
  width: 24px;
  height: 24px;
  line-height: 24px;
  text-align: center;
}
#pause, #play {
  /* Place in bottom-right corner */
  position: absolute;
  z-index: 1;
  right: 10px;
  bottom: 10px;

  /* Button styles */
  border: 1px solid grey;
  vertical-align: center;
  text-align: center;
  background-color: rgba(0,0,0, 0.8);
  cursor: pointer;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>

Splash layer

Create the necessary markup to show two layers that represent the two ad states.

First, we need a <div> that contains a static image, text, and a button to open the video layer.

The buttons contain action handlers that respond to the tap event. After tapping the "Watch Video" button, the splash layer will hide, the video layer will show and the video will play.

Note that the splash layer will collapse on this page, but when previewing the example by itself the layers will occupy the same position.

<div id="splash-container">
  <a href="https://amp.dev/documentation/examples/multimedia-animations/video_ad/"
    target="_blank"
    class="main-link">
    <span class="brand">
      Best Hotels Corp
    </span>
    <span class="title">
      Amazing views from our collection of luxury hotel rooms.
    </span>
    <amp-img src="/static/samples/img/tokyo.jpg"
      width="300"
      height="168">
    </amp-img>
  </a>
  <div role="button"
    class="watch-video"
    on="tap:splash-container.hide,
            video-container.show,
            my-video.play,
            play.hide,
            pause.show">
    Watch Video
  </div>
</div>

Video layer

A <div> that contains the video and buttons to play/pause and close the video layer.

The video is embedded using the amp-video component, which can be controlled by the play and pause actions.

Note how the play and pause buttons are two different elements that are hidden and shown based on playback action.

<div id="video-container"
  hidden>
  <amp-video id="my-video"
    layout="fixed-height"
    height="168"
    src="/static/samples/video/tokyo.mp4"
    loop>
  </amp-video>
  <div role="button"
    class="close"
    on="tap:video-container.hide,
            splash-container.show,
            my-video.pause">
    ×
  </div>
  <div role="button"
    id="pause"
    on="tap:my-video.pause,
            play.show,
            pause.hide">
    ||
  </div>
  <div role="button"
    id="play"
    on="tap:my-video.play,
            play.hide,
            pause.show"></div>
</div>
자세한 설명이 필요하신가요?

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

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

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

GitHub에서 샘플 수정하기