导航

v2.1.0
浮层

灯箱

在原生对话框中放大查看图片。

本页内容

灯箱在页面上方放大显示图片。将 .lightbox 用于原生 <dialog>,缩略图按钮通过 commandfor 和 command="show-modal" 打开灯箱。浏览器负责将灯箱置于顶层、将焦点限制在其中,并支持按 Esc 关闭,整个过程无需脚本。对话框本身没有面板,尺寸与图片完全一致。图片后方的页面会变暗并明显模糊。

灯箱

<button class="card card-interactive w(240)" type="button" commandfor="moonstone" command="show-modal">  <img class="card-media" src="/demo/moonstone.svg" alt>  <span class="card-header">    <span class="card-title">Moonstone</span>    <span class="card-description">View the picture</span>  </span></button><dialog class="lightbox" id="moonstone" closedby="any" aria-label="Moonstone" style="--lightbox-ratio: 16 / 9">  <img class="lightbox-media" src="/demo/moonstone.svg" alt="A pale stone over grey hills">  <p class="lightbox-caption">Moonstone, the stone the light theme is named for.</p>  <button class="btn btn-sm btn-square lightbox-close" type="button" commandfor="moonstone" command="close" aria-label="Close">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M4 4L12 12" /><path d="M4 12L12 4" />    </svg>  </button></dialog>
import XIcon from '@master/icons-react/native/16/soft/x'<>  <button className="card card-interactive w(240)" type="button" {...{ commandfor: 'moonstone', command: 'show-modal' }}>    <img className="card-media" src="/demo/moonstone.svg" alt="" />    <span className="card-header">      <span className="card-title">Moonstone</span>      <span className="card-description">View the picture</span>    </span>  </button>  <dialog className="lightbox" id="moonstone" closedby="any" aria-label="Moonstone" style={{ '--lightbox-ratio': '16 / 9' } as React.CSSProperties}>    <img className="lightbox-media" src="/demo/moonstone.svg" alt="A pale stone over grey hills" />    <p className="lightbox-caption">Moonstone, the stone the light theme is named for.</p>    <button className="btn btn-sm btn-square lightbox-close" type="button" {...{ commandfor: 'moonstone', command: 'close' }} aria-label="Close">      <XIcon className="icon" />    </button>  </dialog></>
<script>  import XIcon from '@master/icons-svelte/native/16/soft/x'</script><button class="card card-interactive w(240)" type="button" commandfor="moonstone" command="show-modal">  <img class="card-media" src="/demo/moonstone.svg" alt>  <span class="card-header">    <span class="card-title">Moonstone</span>    <span class="card-description">View the picture</span>  </span></button><dialog class="lightbox" id="moonstone" closedby="any" aria-label="Moonstone" style="--lightbox-ratio: 16 / 9">  <img class="lightbox-media" src="/demo/moonstone.svg" alt="A pale stone over grey hills">  <p class="lightbox-caption">Moonstone, the stone the light theme is named for.</p>  <button class="btn btn-sm btn-square lightbox-close" type="button" commandfor="moonstone" command="close" aria-label="Close">    <XIcon class="icon" />  </button></dialog>
<script setup>import XIcon from '@master/icons-vue/native/16/soft/x'</script><template>  <button class="card card-interactive w(240)" type="button" commandfor="moonstone" command="show-modal">    <img class="card-media" src="/demo/moonstone.svg" alt>    <span class="card-header">      <span class="card-title">Moonstone</span>      <span class="card-description">View the picture</span>    </span>  </button>  <dialog class="lightbox" id="moonstone" closedby="any" aria-label="Moonstone" style="--lightbox-ratio: 16 / 9">    <img class="lightbox-media" src="/demo/moonstone.svg" alt="A pale stone over grey hills">    <p class="lightbox-caption">Moonstone, the stone the light theme is named for.</p>    <button class="btn btn-sm btn-square lightbox-close" type="button" commandfor="moonstone" command="close" aria-label="Close">      <XIcon class="icon" />    </button>  </dialog></template>
按下卡片打开灯箱。点击图片外部、按下关闭按钮或按 Esc 即可关闭。

#打开与关闭

  • 在缩略图按钮上同时设置 commandfor="id" 和 command="show-modal",即可打开灯箱。在关闭按钮上设置 command="close",即可关闭灯箱并将焦点移回缩略图。
  • closedby="any" 让灯箱支持点击外部关闭。对话框与图片尺寸完全一致,点击图片以外的任意位置都会关闭灯箱。浏览器不支持此属性时,仍可按 Esc 或使用关闭按钮。
  • 如果浏览器不支持调用命令,请在点击事件处理函数中调用 dialog.showModal() 和 dialog.close()。样式表保持不变。

图片通过 @starting-style 和离散过渡从略小的尺寸淡入并放大到目标尺寸,退出比进入更快。缺少这些特性时,图片会立即显示和消失。

#尺寸

将 lightbox-media 用于 <img>、<video> 或 <iframe>。媒体尺寸独立于文件本身。宽高比由 --lightbox-ratio 设置,默认为 3 / 2。媒体四周各预留 24px,宽度最大为 64rem,并按设定的宽高比缩至视口宽高所允许的尺寸。图片、视频和嵌入框架都按同一规则确定尺寸,加载时不会引发布局位移。

在对话框上将 --lightbox-ratio 设为图片本身的宽高比,例如示例中的 16 / 9。宽高比不同的图片会裁剪以填满显示区域。--lightbox-width 设置最大宽度,--lightbox-gutter 设置四周的留白。

#说明文字与关闭按钮

lightbox-caption 是悬浮在图片下方的说明栏,宽度与图片一致,图片会为它留出空间。说明文字控制在一到两行。将 lightbox-close 用于小型方形按钮,按钮位于图片的一个上角,距上边缘和相邻的侧边缘各 8px。按钮采用凸起样式并带有独立轮廓,在任何图片上都容易找到。

灯箱只展示一张图片,无需用户作出选择。带标题、正文或表单的浮层使用对话框。留在页面内并附有说明文字的图片使用插图。

#无障碍

  • 使用 aria-label 为对话框提供无障碍名称,或通过 aria-labelledby 引用其说明文字。
  • 为大图提供能描述图片内容的 alt。如果按钮文字已经说明会打开什么,按钮内缩略图的对应属性可以留空。
  • 始终提供带有无障碍名称的关闭按钮,因为有些用户可能不知道可以点击外部或按 Esc 关闭。
  • 灯箱打开时,焦点移入灯箱,关闭时移回缩略图。启用减少动态效果时,图片只做淡入淡出,尺寸保持不变。

#类名

类名说明
lightbox对话框本身没有面板,尺寸与图片完全一致,底层页面压暗并模糊。
lightbox-media图片、视频或嵌入式框架宽 64rem。视口空间不足时,四周留出 24px 后按可用空间尽量放大,宽高比为 --lightbox-ratio。
lightbox-caption浮在图片下方、与图片等宽的条栏
lightbox-close用于关闭按钮,使其位于图片上角内侧,内缩 8px

#自定义属性

属性默认值说明
--lightbox-ratio3 / 2图片的宽高比,图片按此比例裁剪
--lightbox-width64rem图片的最大宽度。
--lightbox-gutter1.5rem图片与视口各边之间的留白
--lightbox-radiusvar(--radius-control-lg)图片和图注条的圆角。

#源码

@master/ui/components/lightbox.css
/* * Lightbox: a picture opened large over the page. * A native <dialog>, opened as a modal by the thumbnail's own button with commandfor and command="show-modal". * The dialog has no sheet: it is exactly as large as the picture it holds, so a click anywhere else is a * click outside it and, with closedby="any", closes it. The page behind is dimmed and blurred well out of * focus, and the picture stands on it with the shadow of an overlay. * The picture is 64rem wide, or as wide as the viewport lets it be with a 24px gutter on every side, at the * proportions set in --lightbox-ratio, 3 by 2 unless told otherwise. Its size comes from that and never from * the file, so an image, a video and a frame are all sized the same way and nothing moves when one loads. * A caption is a floating bar under the picture, as wide as it is. A close button rests 8px inside its corner. */@layer components.ui {  .lightbox {    --lightbox-width: 64rem;    --lightbox-ratio: 3 / 2;    --lightbox-radius: var(--radius-control-lg);    --lightbox-gutter: 1.5rem;    /* The room a caption takes under the picture. */    --lightbox-room: 0rem;    width: fit-content;    max-width: none;    height: fit-content;    max-height: none;    margin: auto;    padding: 0;    border: 0;    overflow: visible;    background: transparent;    color: var(--color-text-body);    /* Closed is where it leaves to: it fades and settles back half as far as it came, faster than it arrived. */    opacity: 0;    scale: .98;    transition-property: opacity, scale, overlay, display;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);    transition-behavior: allow-discrete;    &[open] {      display: grid;      gap: .5rem;      opacity: 1;      scale: 1;      transition-duration: var(--duration-base);      @starting-style {        opacity: 0;        scale: .96;      }    }    /* The page steps well back: the same scrim as a dialog, with a deeper blur. */    &::backdrop {      background: var(--color-surface-scrim);      backdrop-filter: blur(16px);      opacity: 0;      transition-property: opacity, overlay, display;      transition-duration: var(--duration-quick);      transition-timing-function: var(--easing-glide);      transition-behavior: allow-discrete;    }    &[open]::backdrop {      opacity: 1;      transition-duration: var(--duration-base);    }    /* The scrim's first frame is a rule of its own: a selector that ends in a pseudo-element cannot hold it. */    @starting-style {      &[open]::backdrop {        opacity: 0;      }    }    /* With a caption under it, the picture leaves room for the bar. */    &:has(.lightbox-caption) {      --lightbox-room: 3rem;    }    @media (prefers-reduced-motion: reduce) {      scale: none;      &[open] {        scale: none;        @starting-style {          scale: none;        }      }    }    /* A reader who asks for less transparency gets the scrim without the blur behind it. */    @media (prefers-reduced-transparency: reduce) {      &::backdrop {        backdrop-filter: none;      }    }    @media (forced-colors: active) {      &::backdrop {        background: Canvas;        backdrop-filter: none;      }    }  }  /* The picture, a video or a frame: as wide as its limit, the viewport's width or the viewport's height allows. */  .lightbox-media {    display: block;    width: min(var(--lightbox-width), 100vw - var(--lightbox-gutter) * 2, (100dvh - var(--lightbox-gutter) * 2 - var(--lightbox-room)) * var(--lightbox-ratio));    height: auto;    aspect-ratio: var(--lightbox-ratio);    margin: 0;    border: 0;    border-radius: calc(var(--lightbox-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--color-surface-inset);    box-shadow: var(--shadow-overlay);    outline: 1px solid var(--color-line-image);    outline-offset: -1px;    object-fit: cover;    @media (forced-colors: active) {      outline-color: CanvasText;    }  }  /* The caption: a floating bar as wide as the picture, which never makes the dialog wider than it. */  .lightbox-caption {    box-sizing: border-box;    width: 0;    min-width: 100%;    margin: 0;    padding: .5rem .875rem;    --surface-color: var(--color-surface-floating);    border-radius: calc(var(--lightbox-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--color-surface-floating);    color: var(--color-text-body);    box-shadow: var(--shadow-floating);    font-size: .8125rem;    line-height: 1.45;    text-align: center;    text-wrap: balance;    overflow-wrap: anywhere;    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;    }  }  /* On the close button: it rests inside the picture's top corner. */  .lightbox-close {    position: absolute;    z-index: 1;    inset-block-start: .5rem;    inset-inline-end: .5rem;  }  /* While the picture is open the page behind it does not scroll, as behind a modal dialog. */  :root:has(.lightbox:modal) {    overflow: hidden;  }}