Image 圖片
在原生 <img> 的基礎上加了載入中占位、載入失敗提示、懶載入和大圖預覽。預覽也可以單獨用 HuiImageViewer 打開。
引入
import { HuiImage, HuiImageViewer } from '@nscode/handy-ui'
基礎用法
src 傳圖片地址,width、height 設定尺寸:數字按像素,字串原樣使用(例如 '50%')。只設寬度時高度按圖片比例。
<script setup lang="ts">
import { HuiImage, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap align="start">
<HuiImage src="/images/cat.jpg" alt="一只貓" :width="200" :height="150" />
<HuiImage src="/images/cat.jpg" alt="只設寬度" width="120px" />
</HuiSpace>
</template>
填充方式
fit 決定圖片在容器裡怎麼擺,取值和 CSS 的 object-fit 一樣,預設 'fill'。下面把一張 4:3 的圖片放進 100×100 的方框(灰底是容器):
fill:拉伸鋪滿,圖片變形。contain:完整顯示、保持比例,上下留空。cover:鋪滿、保持比例,左右裁掉。none:按原始尺寸顯示,只能看到中間一塊。scale-down:取none和contain裡較小的一個,這張圖比方框大,所以和contain一樣。
<script setup lang="ts">
import { HuiImage, HuiSpace } from '@nscode/handy-ui'
import type { ImageProps } from '@nscode/handy-ui'
const fits: NonNullable<ImageProps['fit']>[] = ['fill', 'contain', 'cover', 'none', 'scale-down']
</script>
<template>
<HuiSpace wrap size="large">
<HuiSpace v-for="fit in fits" :key="fit" direction="vertical" align="center">
<HuiImage src="/images/cat.jpg" :fit="fit" :width="100" :height="100" class="box" />
<span class="caption">{{ fit }}</span>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.box {
background-color: var(--hui-fill-color);
}
.caption {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
圓形
round 把圖片裁成圓形(border-radius: 50%),寬高相等時是正圓,不相等時是橢圓。
<script setup lang="ts">
import { HuiImage, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap align="center">
<HuiImage src="/images/avatar.jpg" round :width="80" :height="80" />
<HuiImage src="/images/avatar.jpg" round :width="48" :height="48" />
<HuiImage src="/images/cat.jpg" round fit="cover" :width="120" :height="80" />
</HuiSpace>
</template>
載入中和載入失敗
載入中預設顯示轉圈,失敗預設顯示一行文字(隨語言,中文:載入失敗)。placeholder、error 屬性可配置圖片地址,也支援同名插槽,插槽優先。
<script setup lang="ts">
import { HuiImage } from '@nscode/handy-ui'
</script>
<template>
<HuiImage
src="/images/big.jpg"
placeholder="/images/loading.png"
error="/images/broken.png"
:width="200"
:height="150"
/>
</template>
下面第一張地址是錯的,顯示預設的“載入失敗”;第二張地址也是錯的,失敗後顯示 error 指定的圖片:
<script setup lang="ts">
import { HuiImage, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiImage src="/broken.png" :width="160" :height="120" />
<HuiImage
src="/broken.png"
error="/images/empty.png"
fit="contain"
:width="160"
:height="120"
/>
</HuiSpace>
</template>
換了 src 之後會重新顯示載入中,直到新圖片載入完成或失敗。載入成功觸發 load,失敗觸發 error。點按鈕切換地址看看:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiImage, HuiSpace } from '@nscode/handy-ui'
const list = ['/images/1.jpg', '/images/2.jpg', '/broken.png']
const index = ref(0)
const status = ref('載入中')
const next = () => {
index.value = (index.value + 1) % list.length
status.value = '載入中'
}
</script>
<template>
<HuiSpace wrap align="center">
<HuiImage
:src="list[index]"
fit="cover"
:width="200"
:height="120"
@load="status = '觸發了 load'"
@error="status = '觸發了 error'"
/>
<HuiButton @click="next">換一張</HuiButton>
</HuiSpace>
<p style="margin-top: 12px">目前地址:{{ list[index] }},{{ status }}</p>
</template>
懶載入
lazy 開啟後,圖片進入視口(提前 50px)才開始載入,之前一直顯示載入中的占位。不支援 IntersectionObserver 的環境裡直接載入。在下面的框裡往下捲動,圖片依次載入:
<script setup lang="ts">
import { HuiImage, HuiSpace } from '@nscode/handy-ui'
// 地址後面加上序號,每張都單獨請求,方便看出是捲動到了才載入
const images = Array.from({ length: 12 }, (_, i) => `/images/${(i % 3) + 1}.jpg?n=${i + 1}`)
</script>
<template>
<div style="height: 320px; overflow-y: auto">
<HuiSpace wrap>
<HuiImage
v-for="src in images"
:key="src"
:src="src"
fit="cover"
:width="300"
:height="120"
lazy
/>
</HuiSpace>
</div>
</template>
圖片預覽
傳了 preview-src-list 後,點擊圖片(或用 Tab 聚焦後按 Enter、空白)打開預覽。預覽預設從 src 在清單裡的位置開始,initial-index 可以指定從第幾張開始。
預覽裡可以:
- 點左右箭頭或按 ←、→ 切換圖片(到頭後從另一頭接著走)。
- 點工具列縮小、放大、向左旋轉、向右旋轉、還原,或按 ↑、↓ 放大、縮小。
- 放大後按住圖片拖曳。
- 點右上角的關閉按鈕、點圖片外的區域或按 Esc 關閉。
預覽大圖的替代文字:列表只有一張圖時用 alt,多張圖時讀作「第 n 張,共 m 張」。
<script setup lang="ts">
import { HuiImage, HuiSpace } from '@nscode/handy-ui'
const list = ['/images/1.jpg', '/images/2.jpg', '/images/3.jpg']
</script>
<template>
<HuiSpace wrap>
<HuiImage
v-for="(src, index) in list"
:key="src"
:src="src"
:width="100"
:height="75"
:preview-src-list="list"
:initial-index="index"
/>
</HuiSpace>
</template>
不傳 initial-index 時從 src 在清單裡的位置開始;src 不在清單裡時從第一張開始。下面第一張圖打開後先看到 3.jpg,第二張的 src 不在清單裡,打開後先看到 1.jpg:
<script setup lang="ts">
import { HuiImage, HuiSpace } from '@nscode/handy-ui'
const list = ['/images/1.jpg', '/images/2.jpg', '/images/3.jpg']
</script>
<template>
<HuiSpace wrap>
<HuiImage src="/images/3.jpg" fit="cover" :width="120" :height="80" :preview-src-list="list" />
<HuiImage
src="/images/cat.jpg"
fit="cover"
:width="120"
:height="80"
:preview-src-list="list"
/>
</HuiSpace>
</template>
單獨使用預覽
HuiImageViewer 掛載即打開,關閉(離場動畫結束)後發出 close,由使用者用 v-if 卸載它。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiImageViewer } from '@nscode/handy-ui'
const show = ref(false)
const list = ['/images/1.jpg', '/images/2.jpg']
</script>
<template>
<HuiButton @click="show = true">查看大圖</HuiButton>
<HuiImageViewer v-if="show" :url-list="list" hide-on-click-modal @close="show = false" />
</template>
infinite 預設開啟,最後一張往後是第一張;關掉以後到頭時箭頭變灰、不能再切換。initial-index 指定打開時顯示第幾張,切換圖片時觸發 switch。hide-on-click-modal 預設關閉,這時點圖片外的區域不會關閉預覽:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiImageViewer } from '@nscode/handy-ui'
const list = ['/images/1.jpg', '/images/2.jpg', '/images/3.jpg']
const show = ref(false)
const current = ref<number>()
const open = () => {
current.value = undefined
show.value = true
}
</script>
<template>
<HuiButton @click="open">從第 2 張打開,不循環</HuiButton>
<p style="margin-top: 12px">
{{ current === undefined ? '還沒有切換' : `switch 觸發,切到了第 ${current + 1} 張` }}
</p>
<HuiImageViewer
v-if="show"
:url-list="list"
:initial-index="1"
:infinite="false"
@switch="current = $event"
@close="show = false"
/>
</template>
預覽語義屬性與原生事件
HuiImageViewer 的可見全螢幕外層同時是 role="dialog":class、style 用於外觀,id、aria-*、tabindex 和原生 DOM 監聽也交給這個對話框節點。屬性可以動態更新或移除;顯式 aria-label="" 保留空名稱,移除該屬性才恢復語言包名稱。
下面使用離線圖片。點擊下一張時,原按鈕照常切圖,名稱隨編號更新,原生 click 只通知一次;event.currentTarget 是對話框,event.target 仍是實際點擊的子節點。關閉後可查看計數。
<script setup lang="ts">
import { ref, useId } from 'vue'
import { HuiButton, HuiImageViewer, HuiSpace } from '@nscode/handy-ui'
const show = ref(false)
const active = ref(0)
const clicks = ref(0)
const target = ref('尚未點擊')
const previewId = useId()
const helpId = useId()
const images = [1, 2].map(
(index) =>
'data:image/svg+xml;charset=utf-8,' +
encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" width="400" height="240"><rect width="400" height="240" fill="' +
(index === 1 ? '#409eff' : '#67c23a') +
'"/><text x="200" y="135" text-anchor="middle" fill="white" font-size="64">' +
index +
'</text></svg>'
)
)
function open() {
active.value = 0
show.value = true
}
function onPreviewClick(event: MouseEvent) {
clicks.value++
target.value = (event.currentTarget as HTMLElement).getAttribute('role') ?? ''
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="open">打開帶描述的預覽</HuiButton>
<p :id="helpId">左右鍵或箭頭按鈕切圖,預覽名稱隨圖片編號更新。</p>
<p>原生點擊:{{ clicks }};監聽目標:{{ target }}</p>
<HuiImageViewer
v-if="show"
:id="previewId"
class="preview-appearance"
style="outline: 2px solid var(--hui-color-primary); outline-offset: -2px"
:url-list="images"
:aria-label="'附件預覽 ' + (active + 1) + ' / ' + images.length"
:aria-describedby="helpId"
data-gallery="attachments"
@click="onPreviewClick"
@switch="active = $event"
@close="show = false"
/>
</HuiSpace>
</template>
占位和錯誤插槽
自訂插槽優先於同名圖片地址屬性;示例使用無效圖片演示失敗內容。
<script setup lang="ts">
import { HuiImage } from '@nscode/handy-ui'
</script>
<template>
<HuiImage src="data:image/png;base64,broken" :width="200" :height="100"
><template #placeholder>正在載入圖片…</template
><template #error>圖片無法顯示,請檢查圖片地址</template></HuiImage
>
</template>
HuiImage Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| src | string | - | 圖片地址,必填 |
| alt | string | '' | 替代文字;預覽只有一張圖時也用作大圖的替代文字 |
| fit | 'fill' | 'contain' | 'cover' | 'none' | 'scale-down' | 'fill' | 圖片在容器裡的適應方式,同 CSS 的 object-fit |
| lazy | boolean | false | 是否懶載入,進入視口(提前 50px)後才載入 |
| previewSrcList | string[] | - | 預覽圖片清單。傳了(並且不為空)才能點擊預覽 |
| initialIndex | number | - | 打開預覽時顯示第幾張(從 0 開始,超出範圍時取最近的一張);不傳時從 src 所在位置開始 |
| zIndex | number | - | 圖片預覽層級,不傳時跟隨 ConfigProvider,預設 2000 |
| width | string | number | - | 寬度,數字按像素,字串原樣使用 |
| height | string | number | - | 高度,數字按像素,字串原樣使用 |
| round | boolean | false | 是否顯示成圓形(border-radius: 50%) |
| placeholder | string | - | 載入中顯示的占位圖地址;不傳時顯示轉圈 |
| error | string | - | 載入失敗時顯示的圖片地址;不傳時顯示隨語言的文字(中文:載入失敗) |
HuiImage Events
| 事件 | 回呼參數 | 說明 |
|---|---|---|
| load | (event: Event) | 圖片載入成功時觸發 |
| error | (event: Event) | 圖片載入失敗時觸發 |
HuiImage Slots
| 插槽 | 參數 | 說明 |
|---|---|---|
| placeholder | - | 載入中內容,優先於 placeholder 圖片屬性 |
| error | - | 載入失敗內容,優先於 error 圖片屬性 |
HuiImageViewer Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| urlList | string[] | [] | 預覽的圖片清單 |
| initialIndex | number | 0 | 打開時顯示第幾張 |
| zIndex | number | - | 圖片預覽層級,不傳時跟隨 ConfigProvider,預設 2000 |
| infinite | boolean | true | 是否循環切換:第一張往前是最後一張,反之亦然 |
| hideOnClickModal | boolean | false | 點圖片外的區域是否關閉預覽(HuiImage 裡總是開啟) |
| alt | string | - | 大圖的替代文字,只在 urlList 只有一張圖時使用;多張圖或不傳時讀作「第 n 張,共 m 張」 |
HuiImageViewer Events
| 事件 | 回呼參數 | 說明 |
|---|---|---|
| close | - | 預覽關閉、離場動畫結束後觸發,這時可以用 v-if 卸載它 |
| switch | (index: number) | 切換圖片後觸發,參數是新的下標 |
類型
import type { ImageProps, ImageViewerProps, ImageViewerEmits } from '@nscode/handy-ui'
interface ImageProps {
src: string
alt?: string
fit?: 'fill' | 'contain' | 'cover' | 'none' | 'scale-down'
lazy?: boolean
previewSrcList?: string[]
initialIndex?: number
zIndex?: number
width?: string | number
height?: string | number
round?: boolean
placeholder?: string
error?: string
}
interface ImageViewerProps {
urlList: string[]
initialIndex?: number
zIndex?: number
infinite?: boolean
hideOnClickModal?: boolean
alt?: string
}
interface ImageViewerEmits {
close: []
switch: [index: number]
}