網站導覽

v2.1.0
覆疊元件

燈箱

在原生對話框中放大圖片,顯示於頁面上方。

本頁內容

燈箱會在頁面上方放大顯示圖片。在原生 <dialog> 上使用 .lightbox。縮圖按鈕本身透過 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>。尺寸由版面配置決定。寬度上限為 64rem,並依檢視區的寬度與高度縮小,四周各留 24px。長寬比由 --lightbox-ratio 設定,預設為 3 / 2。因此,圖片、影片和內嵌框架都以相同方式決定尺寸,載入時也不會造成版面位移。

在對話框上設定 --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;  }}