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

amp-lightbox-gallery

Introduction

The amp-lightbox-gallery component provides a "lightbox” experience for AMP components (e.g., amp-img, amp-carousel). When the user interacts with the AMP element, a UI component expands to fill the viewport until it is closed by the user. Currently, only images are supported.

Setup

Import the amp-lightbox-gallery component in the header.

<script async custom-element="amp-lightbox-gallery" src="https://cdn.ampproject.org/v0/amp-lightbox-gallery-0.1.js"></script>

Optionally import the amp-carousel component in the header if you are using lightbox with carousel.

<script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script>

Basic usage

This is a basic example that demonstrates lightboxed <amp-img>s. You have one or more <amp-img> elements on the page. Just add the "lightbox" attribute to each image that you wish to view in a lightbox.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

<amp-img lightbox
  src="/static/samples/img/road-1.jpg"
  width="300"
  height="200"
  layout="responsive"></amp-img>
<p class="paragraph">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<amp-img lightbox
  src="/static/samples/img/road-2.jpg"
  width="300"
  height="200"
  layout="responsive"></amp-img>
<p class="paragraph">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<amp-img lightbox
  src="/static/samples/img/road-3.jpg"
  width="300"
  height="200"
  layout="responsive"></amp-img>
<p class="paragraph">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

You can also use <amp-lightbox-gallery> with <amp-carousel> to view the contents of the carousel in a lightbox. Just simply include a <amp-carousel> with <amp-img>s, and add the "lightbox" attribute to the <amp-carousel>. The lightbox images will be sync-ed with the carousel slides.

<amp-carousel lightbox
  width="400"
  height="300"
  layout="responsive"
  type="slides">
  <amp-img src="/static/samples/img/image1.jpg"
    width="400"
    height="300"
    layout="responsive"></amp-img>
  <amp-img src="/static/samples/img/image2.jpg"
    width="400"
    height="300"
    layout="responsive"></amp-img>
  <amp-img src="/static/samples/img/image3.jpg"
    width="400"
    height="300"
    layout="responsive"></amp-img>
</amp-carousel>

By default, all lightboxed elements will be grouped into the same lightbox unless they belong to a carousel. All elements in the same lightboxed carousel belong to the same lightbox. You can override this behavior by setting the lightbox attribute to a unique string. One common pattern is to have one hero image that opens up into a lightbox gallery of several images.

See photo gallery
<amp-img lightbox="hero"
  src="/static/samples/img/clean-2.jpg"
  width="400"
  height="300"
  layout="responsive">
  <span class="overlay-text">See photo gallery</span>
</amp-img>
<div hidden>
  <amp-img lightbox="hero"
    src="/static/samples/img/clean-1.jpg"
    layout="responsive"
    width="400"
    height="710"></amp-img>
  <amp-img lightbox="hero"
    src="/static/samples/img/clean-3.jpg"
    width="400"
    height="300"
    layout="responsive"></amp-img>
</div>

Captions

Optionally, you can specify a caption for each element in the lightbox. These fields are automatically read and displayed by the <amp-lightbox-gallery> in the following order of priority: - figcaption (if the lightboxed element is the child of a figure) - aria-describedby The following examples demonstrate how to use captions. The first image does not have a caption. The second image uses <figcaption>. The third image uses aria-describedby.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

This is a long road. We don't know where it leads.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

<amp-img lightbox="caption"
  src="/static/samples/img/road-1.jpg"
  width="300"
  height="200"
  layout="responsive"></amp-img>
<p class="paragraph">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

<figure class="sample-figure">
  <amp-img lightbox="caption"
    src="/static/samples/img/road-2.jpg"
    width="300"
    height="200"
    layout="responsive"></amp-img>
  <figcaption>
    This is a long road. We don't know where it leads.
  </figcaption>
</figure>
<p class="paragraph">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

<amp-img lightbox="caption"
  src="/static/samples/img/road-3.jpg"
  layout="responsive"
  width="300"
  height="200"
  aria-describedby="desc">
</amp-img>
<p class="paragraph">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

<div hidden
  id="desc">
  I'm not too sure where this road leads either.
</div>
需要进一步说明?

如果此页面的说明未能涵盖您的所有问题,欢迎与其他 AMP 用户取得联系,讨论您的具体用例。

前往 Stack Overflow
一项无法解释的功能?

AMP 项目强烈鼓励您参与其中并作出贡献!我们希望您能成为我们开源社区的持续参与者,但我们也欢迎您对热衷问题做出单次贡献。

在 GitHub 上编辑实例