覆疊元件
燈箱
在原生對話框中放大圖片,顯示於頁面上方。
本頁內容
燈箱會在頁面上方放大顯示圖片。在原生 <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-ratio | 3 / 2 | 圖片的寬高比,以寬度除以高度表示。圖片會依此比例裁切。 |
--lightbox-width | 64rem | 圖片的最大寬度 |
--lightbox-gutter | 1.5rem | 圖片與檢視區各邊之間保留的空間 |
--lightbox-radius | var(--radius-control-lg) | 圖片與其說明的圓角 |