AMP

重要提示:此文档不适用于您当前选择的格式 email

amp-lightbox-gallery

 
现在您可以使用此组件,在有效的 AMP 文档之外使用此组件的 Bento 版本。在 Bento 指南 中了解更多信息。

描述

提供“灯箱”体验。用户交互后,UI 组件将扩展以填充视口,直到用户关闭它。

 

必需脚本

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

用法

amp-lightbox-gallery 组件为 AMP 组件(例如 amp-imgamp-carousel)提供“灯箱”体验。当用户与 AMP 元素交互时,UI 组件会扩展以填充视口,直到用户关闭它。目前,仅支持图像。

要使用 amp-lightbox-gallery,请确保在 <head> 部分包含必需的脚本,然后在 <amp-img><amp-carousel> 元素上添加 lightbox 属性。

对于在灯箱中显示单个图像,还有 <amp-image-lightbox> 组件。要在灯箱中显示其他类型的元素,请使用 <amp-lightbox>

使用 <amp-img> 显示灯箱

点击任何 <amp-img> 会在灯箱画廊中打开图像。灯箱画廊处理图像(例如,缩放和平移),允许滑动以在图像之间导航,并提供一个缩略图画廊视图,用于浏览网格中的所有图片缩略图。

<amp-img src="cat.jpg" width="100" height="100" lightbox></amp-img>
<amp-img src="dog.jpg" width="100" height="100" lightbox></amp-img>
<amp-img src="bird.jpg" width="100" height="100" lightbox></amp-img>

使用 <amp-carousel> 显示灯箱

您可以在 <amp-carousel> 上添加 lightbox 属性以灯箱其所有子项。目前,<amp-lightbox-gallery> 组件仅支持包含 <amp-img> 作为子项的轮播。当您在灯箱中浏览轮播项时,原始轮播幻灯片会同步,以便在灯箱关闭时,用户最终停留在他们最初所在的同一张幻灯片上。目前,仅支持 type='slides' 轮播。

<amp-carousel
  lightbox
  width="1600"
  height="900"
  layout="responsive"
  type="slides"
>
  <amp-img src="image1" width="200" height="100"></amp-img>
  <amp-img src="image1" width="200" height="100"></amp-img>
  <amp-img src="image1" width="200" height="100"></amp-img>
</amp-carousel>

添加标题

(可选)您可以为灯箱中的每个元素指定标题。这些字段由 <amp-lightbox-gallery> 自动读取并按以下优先级顺序显示

  • figcaption(如果灯箱元素是 figure 的子元素)
  • aria-describedby
  • alt
  • aria-label
  • aria-labelledby

在以下示例中,<amp-lightbox-gallery> 显示 figcaption 值作为其描述,显示“多伦多的 CN 塔是 ...”。

<figure>
  <amp-img
    id="hero-img"
    lightbox="toronto"
    src="https://picsum.photos/1600/900?image=1075"
    layout="responsive"
    width="1600"
    height="900"
    alt="Picture of CN tower."
  >
  </amp-img>
  <figcaption class="image">
    Toronto's CN tower was built in 1976 and was the tallest free-standing
    structure until 2007.
  </figcaption>
</figure>

在以下示例中,<amp-lightbox-gallery> 显示 alt 值作为其描述,显示“CN 塔的图片”。

<amp-img
  id="hero-img"
  lightbox="toronto"
  src="https://picsum.photos/1600/900?image=1075"
  layout="responsive"
  width="1600"
  height="900"
  alt="Picture of CN tower"
>
</amp-img>

实现缩略图预览

灯箱项目具有缩略图画廊视图。您可以选择通过属性 lightbox-thumbnail-id 为您的灯箱元素指定缩略图项目,该属性引用带有 layout="nodisplay"<amp-img> 元素的 id

<amp-youtube
  width="480"
  height="270"
  layout="responsive"
  data-videoid="lBTCB7yLs8Y"
  lightbox-thumbnail-id="my-thumbnail-img"
>
</amp-youtube>

<amp-img
  id="my-thumbnail-img"
  width="200"
  height="200"
  layout="nodisplay"
  src="https://picsum.photos/200/200?image=1074"
>
</amp-img>

如果未指定缩略图,则 <amp-img> 元素将按照 object-fit: cover 进行裁剪,<amp-video> 将使用其 poster 属性中指定的图像 src,并且对于具有占位符图像的灯箱元素,将使用占位符图像。

样式

您可以通过使用标准 CSS 定位 amp-lightbox-gallery-caption 类来设置 amp-lightbox-gallery 组件的标题样式。可以设置样式的部分属性包括 font-sizecolor

操作

打开

打开灯箱画廊。如果您指定图像 id,则可以通过点击其他元素触发:on="tap:amp-lightbox-gallery.open(id='image-id')"

分析

要跟踪 amp-lightbox-gallery 的使用情况,请使用以下分析事件之一

lightboxOpened

当用户单击灯箱 <amp-img> 时,此事件会跟踪灯箱何时打开。

您可以使用以下代码段跟踪此事件

<amp-analytics>
  <script type="application/json">
    {
      "requests": {
        "open": "https://foo.com/open"
      },
      "triggers": {
        "trackLightboxOpened": {
          "on": "lightboxOpened",
          "request": "open"
        }
      }
    }
  </script>
</amp-analytics>

thumbnailsViewToggled

当用户在灯箱视图中单击触发器时,此事件会跟踪何时触发缩略图视图。

您可以使用以下代码段跟踪此事件

<amp-analytics>
  <script type="application/json">
    {
      "requests": {
        "thumb": "https://foo.com/thumb"
      },
      "triggers": {
        "trackThumbnailsViewToggled": {
          "on": "thumbnailsViewToggled",
          "request": "thumb"
        }
      }
    }
  </script>
</amp-analytics>

descriptionOverflowToggled

当用户通过单击描述来展开/折叠描述时,此事件会跟踪,跟踪与灯箱图像的描述的互动情况。

您可以使用以下代码段跟踪此事件

<amp-analytics>
  <script type="application/json">
    {
      "requests": {
        "descOverflow": "https://foo.com/descOverflow"
      },
      "triggers": {
        "trackDescriptionOverflowed": {
          "on": "descriptionOverflowToggled",
          "request": "descOverflow"
        }
      }
    }
  </script>
</amp-analytics>

验证

请参阅 AMP 验证器规范中的 amp-image-lightbox-gallery 规则

需要更多帮助?

您已经阅读此文档十几次了,但它并没有真正涵盖您所有的问题?也许其他人也有同样的感觉:在 Stack Overflow 上联系他们。

转到 Stack Overflow
发现错误或缺少功能?

AMP 项目强烈鼓励您的参与和贡献!我们希望您能成为我们开源社区的长期参与者,但我们也欢迎您对您特别感兴趣的问题做出一次性贡献。

转到 GitHub