amp-lightbox-gallery
描述
提供“灯箱”体验。在用户交互后,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-img
、amp-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
值作为其描述,显示“Toront's CN tower was ....”。
<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>
实现缩略图预览
带灯箱的项目具有缩略图画廊视图。你可以选择通过引用 <amp-img>
元素的 id
的属性 lightbox-thumbnail-id
为你的带灯箱的元素指定缩略图项目,该元素的 layout="nodisplay"
。
<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>
如果未指定缩略图,则将按照 object-fit: cover
裁剪 <amp-img>
元素,<amp-video>
将使用其 poster
属性中指定的图像 src
,并且将对带有占位符图像的带灯箱的元素使用占位符图像。
样式
你可以通过使用标准 CSS 定位 amp-lightbox-gallery-caption
类来设置 amp-lightbox-gallery
组件的标题样式。可以设置样式的属性包括 font-size
和 color
。
操作
打开
打开灯箱画廊。如果你指定了图像 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