AMP
  • ads

Scrollbound Animation Ad

Summary

Sample AMPHTML ad using amp-position-observer, and amp-animation to create an interactive ad.

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>
  /* Main element that contains the creative. */
  .root-container {
    background: #000;
    color: #fff;
    font-family: 'Roboto', sans-serif;
    font-size: 12px;
    overflow: hidden;
    width: 340px;
    height: 240px;
    position: relative;
    margin: 0 auto;
    display: block;
  }
  /* Position the footer absolutely, at the bottom of the container. */
  .footer {
    background: #000;
    font-size: 12px;
    padding: 8px;
    text-transform: uppercase;
    display: flex;
    align-items: center;
    color: #fff;
    z-index: 2;
    position: absolute;
    left: 0;
    bottom: 0;
    right: 0;
  }
  .footer-logo {
    border-right: 1px solid #fff;
    padding: 0 8px 0 0;
    margin: 0 8px 0 0;
  }
  .logo-img {
    display: block;
  }
  .stretch {
    flex: 1;
  }
  .button {
    text-transform: uppercase;
    padding: 8px;
    color: #fff;
    display: inline-block;
    background-color: #2979ff;
    text-decoration: none;
  }
  .button:active {
    background-color: #92bbff;
  }
  /* Basic cards styling */
  .cards-container {
    position: absolute;
    z-index: 1;
    left: 0;
    right: 0;
  }
  .card {
    width: 100%;
    overflow: hidden;
    position: relative;
  }
  /* Change position of the expand button for the middle card */
  .card-gauges .expand-button {
    left: 90px;
    top: 50px;
  }
  .learn-more a:active {
    background-color: #f0f0f0;
  }
</style>

Animations

The animations are implemented using amp-animation which is an AMP component that uses Web Animations API to support both time and scroll based animations.

<amp-animation id="adAnim" layout="nodisplay">
  <script type="application/json">
    {
      "duration": "1s",
      "fill": "both",
      "direction": "alternate",
      "animations": [
        {
          "selector": "#cardsContainer",
          "keyframes": [
            { "transform": "translateY(0)" },
            { "transform": "translateY(-384px)"}
          ]
        }
      ]
    }
  </script>
</amp-animation>

Slide cards opposite to scroll direction: The ad initially shows the top-most card. As the document scrolls, the other two cards are revealed.

To achieve that, we define 4 keyframes. Between 0% and 10% of viewport, the first card is displayed. Between 10% and 90%, the cards container will vertically translate to -384 pixels, which is the negative of double as each card's height. When reaching this point, the third card will be fully displayed. Between 90% and 100%, the same position is kept.

Scrollbound Animations

The scrollbound animations are implemented using amp-position-observer which is an AMP component that Monitors the position of an element within the viewport as a user scrolls, and dispatches enter, exit and scroll events that can be used with other components

<amp-position-observer
  intersection-ratios="1"
  viewport-margins="10vh"
  on="scroll:adAnim.seekTo(percent=event.percent)"
  layout="nodisplay">
</amp-position-observer>
<div class="root-container">
 <div class="footer">
   <div class="footer-logo">
     <amp-img src="/static/samples/img/car-logo.png"
         width="72"
         height="32"
         layout="fixed"
         alt="Howdy"
         class="logo-img"></amp-img>
   </div>
   <div class="stretch">The All-New WX-S R5</div>
   <a href="https://amp.dev/documentation/examples/" target="_blank" class="button">
     Learn more
   </a>
 </div>

Cards

<div class="cards-container" id="cardsContainer">
  <div class="card card-seats">
    <amp-img src="/static/samples/img/car-seats.jpg"
        width="340"
        height="192"
        layout="responsive"
        alt="Ergonomic Leather Seats"
        class="a-carousel-img"></amp-img>
  </div>
  <div class="card card-gauges">
    <amp-img src="/static/samples/img/car-gauges.jpg"
        width="340"
        height="192"
        layout="responsive"
        alt="Sport Gauge Cluster"
        class="a-carousel-img"></amp-img>
  </div>
  <div class="card card-engine">
    <amp-img src="/static/samples/img/car-engine.jpg"
        width="340"
        height="192"
        layout="responsive"
        alt="Tuned Engine"
        class="a-carousel-img"></amp-img>
  </div>
</div>
자세한 설명이 필요하신가요?

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

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

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

GitHub에서 샘플 수정하기