Scrollbound Lightbox Ad

Edit on Github
Open in Playground View Demo

Experimental Mode

This example uses the following experimental features: [amp-animationamp-lightbox-a4a-proto]. Enable the experiment via the button below. Some components require the AMP Dev Channel to be enabled as well. Learn more here.

Enable Dev Channel

Summary

Sample AMP Ad using amp-lightbox, 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;
  }
  /* Position expand button absolutely on top of the card */
  .expand-button {
    padding: 0;
    width: 44px;
    height: 44px;
    line-height: 44px;
    text-align: center;
    position: absolute;
    z-index: 1;
    left: 40%;
    top: 40%;
    font-size: 24px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.8);
  }
  /* Change position of the expand button for the middle card */
  .card-gauges .expand-button {
    left: 90px;
    top: 50px;
  }
  /* Lightbox styling */
  amp-lightbox {
    font-family: 'Roboto', sans-serif;
    font-size: 12px;
    background: rgba(0, 0, 0, 0.8);
  }
  .lightbox {
    height: 100%;
    margin: 0 auto 0;
    max-width: 600px;
    background: #fff;
    color: #888;
    overflow: hidden;
    position: relative;
    display: flex;
    flex-direction: column;
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.3);
  }
  .lightbox-main {
    flex: 1;
    display: flex;
    max-width: 340px;
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 30px 20px;
    line-height: 20px;
    align-self: center;
  }
  .lightbox-main p {
    margin: 5px 0;
  }
  .lightbox-main h3 {
    text-transform: uppercase;
    margin: 10px auto 0;
    font-weight: normal;
    letter-spacing: 2px;
    font-size: 14px;
    color: #000;
    align-self: center;
    max-width: 320px;
  }
  /* Creates decorative underline for h3 headers */
  .lightbox-main h3:after {
    display: block;
    content: " ";
    background-color: #2979ff;
    width: 20px;
    height: 3px;
    margin: 12px auto 10px;
  }
  .lightbox-close-container {
    text-align: center;
    border-top: 1px solid #eee;
    padding: 18px 0;
  }
  .lightbox-header {
    background: #000;
    text-align: center;
    padding: 10px 0;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .lightbox-header h1  {
    display: inline;
    color: #fff;
    text-transform: uppercase;
    font-weight: normal;
    font-size: 14px;
    border-left: 1px solid #fff;
    margin: 0 0 0 12px;
    padding-left: 12px;
  }
  .lightbox-hero {
    position: relative;
    overflow: hidden;
  }
  .learn-more a {
    padding: 8px;
    text-transform: uppercase;
    display: inline-block;
    color: #2979ff;
    text-decoration: none;
    font-weight: bold;
  }
  .learn-more a:active {
    background-color: #f0f0f0;
  }
  /* Responsive styles so that the lightbox does not over-stretch */
  @media (max-height: 500px) {
    .lightbox {
      max-width: 400px;
    }
    .lightbox-hero {
      max-height: 160px;
    }
    .lightbox-hero-img {
      margin-top: -30px;
    }
  }
  @media (max-height: 360px) {
    .lightbox-hero-img {
      display: none;
    }
    .lightbox-header {
      position: static;
    }
  }
</style>

Scrollbound Animations

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

<amp-animation layout="nodisplay"
  trigger="visibility">
  <script type="application/json">
    {
      "ticker": "scroll",
      "fill": "both",
      "animations": [{
        "target": "cardsContainer",
        "keyframes": [{
            "transform": "translateY(0)"
          },
          {
            "transform": "translateY(0)",
            "offset": 0.1
          },
          {
            "transform": "translateY(-384px)",
            "offset": 0.9
          },
          {
            "transform": "translateY(-384px)",
            "offset": 1
          }
        ]
      }]
    }
  </script>
</amp-animation>

scroll vs. time ticker: Setting "ticker": "scroll" tells amp-animation to advance the animation timeline based on scrolling rather than time.

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.

amp-ad-banner is a dummy AMP element that is used by the AMP runtime to determine the position of the ad when a lightbox is opened.

Please note that amp-ad-banner is still in experimental stages and is likely to change in the future. See this Github issue for tracking.

<amp-ad-banner class="root-container"
  layout="container">
  <div class="footer">
    <div class="footer-logo">
      <amp-img src="https://ampbyexample.com/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://ampbyexample.com/"
      target="_blank"
      class="button">
      Learn more
    </a>
  </div>

Cards

AMP has a system in place for events and actions. It uses a domain-specific language to describe how actions are triggered. In this example, we set the on attribute so the lightbox will activate when a card button is tapped.

Each button opens the lightbox related to the card that triggers it.

<div class="cards-container"
  id="cardsContainer">
  <div class="card card-seats">
    <div class="button expand-button"
      role="button"
      on="tap:lightbox-seats.activate">+</div>
    <amp-img src="https://ampbyexample.com/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">
    <div class="button expand-button"
      role="button"
      on="tap:lightbox-gauges.activate">+</div>
    <amp-img src="https://ampbyexample.com/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">
    <div class="button expand-button"
      role="button"
      on="tap:lightbox-engine.activate">+</div>
    <amp-img src="https://ampbyexample.com/img/car-engine.jpg"
      width="340"
      height="192"
      layout="responsive"
      alt="Tuned Engine"
      class="a-carousel-img"></amp-img>
  </div>
</div>

Lightboxes

The amp-lightbox component defines the child elements that will be displayed in a full-viewport overlay. We set a different id for each lightbox so they can be opened and closed by actions on other elements. Its content is normal AMP HTML.

<amp-lightbox id="lightbox-seats"
  layout="nodisplay">
  <div class="lightbox">
    <div class="lightbox-hero">
      <amp-img src="https://ampbyexample.com/img/car-seats.jpg"
        width="608"
        height="342"
        layout="responsive"
        alt="Ergonomic Leather Seats"
        class="lightbox-hero-img"></amp-img>
      <div class="lightbox-header">
        <amp-img src="https://ampbyexample.com/img/car-logo.png"
          width="72"
          height="32"
          layout="fixed"
          alt="Howdy"
          class="logo-img"></amp-img>
        <h1>The all-new WX-S R5</h1>
      </div>
    </div>
    <div class="lightbox-main">
      <h3>Ergonomic Leather Seats</h3>
      <p>
        Available in Comfort Suede and leather. High-contrast stitching colors available.
      </p>
      <p class="learn-more">
        <a href="https://ampbyexample.com/"
          target="_blank">Learn more</a>
      </p>
    </div>
    <div class="lightbox-close-container">
      <div class="button lightbox-close-button"
        role="button"
        on="tap:lightbox-seats.close">
        Close
      </div>
    </div>
  </div>
</amp-lightbox>

The second lightbox is almost identical to the first one, but with different content.

<amp-lightbox id="lightbox-gauges"
  layout="nodisplay">
  <div class="lightbox">
    <div class="lightbox-hero">
      <amp-img src="https://ampbyexample.com/img/car-gauges.jpg"
        width="608"
        height="342"
        layout="responsive"
        alt="Sports Gauge Cluster"
        class="lightbox-hero-img"></amp-img>
      <div class="lightbox-header">
        <amp-img src="https://ampbyexample.com/img/car-logo.png"
          width="72"
          height="32"
          layout="fixed"
          alt="Howdy"
          class="logo-img"></amp-img>
        <h1>The all-new WX-S R5</h1>
      </div>
    </div>
    <div class="lightbox-main">
      <h3>Sports Gauge Cluster</h3>
      <p>
        Sports-car inspired illuminated gauge cluster.
      </p>
      <p class="learn-more">
        <a href="https://ampbyexample.com/"
          target="_blank">Learn more</a>
      </p>
    </div>
    <div class="lightbox-close-container">
      <div class="button lightbox-close-button"
        role="button"
        on="tap:lightbox-gauges.close">
        Close
      </div>
    </div>
  </div>
</amp-lightbox>

...and the third one as well.

<amp-lightbox id="lightbox-engine"
  layout="nodisplay">
  <div class="lightbox">
    <div class="lightbox-hero">
      <amp-img src="https://ampbyexample.com/img/car-engine.jpg"
        width="608"
        height="342"
        layout="responsive"
        alt="Tuned Engine"
        class="lightbox-hero-img"></amp-img>
      <div class="lightbox-header">
        <amp-img src="https://ampbyexample.com/img/car-logo.png"
          width="72"
          height="32"
          layout="fixed"
          alt="Howdy"
          class="logo-img"></amp-img>
        <h1>The all-new WX-S R5</h1>
      </div>
    </div>
    <div class="lightbox-main">
      <h3>Tuned Engine</h3>
      <p>
        3.5L 6-cylinder engine, 290 horsepower. Lightweight aluminum-aloy block and cylinder head.
      </p>
      <p class="learn-more">
        <a href="https://ampbyexample.com/"
          target="_blank">Learn more</a>
      </p>
    </div>
    <div class="lightbox-close-container">
      <div class="button lightbox-close-button"
        role="button"
        on="tap:lightbox-engine.close">
        Close
      </div>
    </div>
  </div>
</amp-lightbox>