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 聚焦后按回车、空格)打开预览。预览默认从 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]
}