图片组件 Image
可预览的图片容器,并支持懒加载,自定义占位、加载失败等
基础用法
可通过 fit
来设置图片在容器的样式,同原生 object-fit
。
fill
contain
cover
none
scale-down
<template>
<div flex>
<div v-for="fit in fitList" :key="fit" style="flex: 1; padding: 10px">
<div style="width: 100px; height: 80px; border: 1px solid #eee">
<b-image :src="url" :fit="fit" width="100px" height="80px" :alt="fit"></b-image>
</div>
<p>{{ fit }}</p>
</div>
</div>
</template>
<script setup>
const url = 'https://file.iviewui.com/images/image-demo-3.jpg'
const fitList = ['fill', 'contain', 'cover', 'none', 'scale-down']
</script>
<template>
<div flex>
<div v-for="fit in fitList" :key="fit" style="flex: 1; padding: 10px">
<div style="width: 100px; height: 80px; border: 1px solid #eee">
<b-image :src="url" :fit="fit" width="100px" height="80px" :alt="fit"></b-image>
</div>
<p>{{ fit }}</p>
</div>
</div>
</template>
<script setup>
const url = 'https://file.iviewui.com/images/image-demo-3.jpg'
const fitList = ['fill', 'contain', 'cover', 'none', 'scale-down']
</script>
加载中
可以设置加载中的文字或者占位内容
默认
插槽
<template>
<b-row gutter="20">
<b-col span="12">
<b-image src="https://wangbin3162.github.io/bin-files/bg/bg2.jpg" />
<p class="t-center">默认</p>
</b-col>
<b-col span="12">
<b-image src="https://wangbin3162.github.io/bin-files/bg/bg3.jpg">
<template #placeholder>
<b-loading fix />
</template>
</b-image>
<p class="t-center">插槽</p>
</b-col>
</b-row>
</template>
<template>
<b-row gutter="20">
<b-col span="12">
<b-image src="https://wangbin3162.github.io/bin-files/bg/bg2.jpg" />
<p class="t-center">默认</p>
</b-col>
<b-col span="12">
<b-image src="https://wangbin3162.github.io/bin-files/bg/bg3.jpg">
<template #placeholder>
<b-loading fix />
</template>
</b-image>
<p class="t-center">插槽</p>
</b-col>
</b-row>
</template>
加载失败
可以设置加载失败占位内容。
默认
插槽
<template>
<b-row gutter="20">
<b-col span="12">
<div flex="main:center">
<b-image width="200px" height="100px" />
</div>
<p class="t-center">默认</p>
</b-col>
<b-col span="12">
<div flex="main:center">
<b-image width="200px" height="100px">
<template #error>
<b-icon name="image" size="28" color="var(--bin-fill-color-4)" />
</template>
</b-image>
</div>
<p class="t-center">插槽</p>
</b-col>
</b-row>
</template>
<template>
<b-row gutter="20">
<b-col span="12">
<div flex="main:center">
<b-image width="200px" height="100px" />
</div>
<p class="t-center">默认</p>
</b-col>
<b-col span="12">
<div flex="main:center">
<b-image width="200px" height="100px">
<template #error>
<b-icon name="image" size="28" color="var(--bin-fill-color-4)" />
</template>
</b-image>
</div>
<p class="t-center">插槽</p>
</b-col>
</b-row>
</template>
懒加载
设置属性 lazy
可以开启图片懒加载功能,当图片可视时才会加载。
设置属性 scroll-container
可以指定滚动容器,如未设置,会自动寻找最近一个 overflow
值为 auto
或 scroll
的父元素。
纵向
横向
<template>
<b-row gutter="20">
<b-col span="12">
<div class="demo-image-lazy-vertical">
<b-image v-for="url in vUrlList" :key="url" :src="url" lazy />
</div>
<p class="t-center">纵向</p>
</b-col>
<b-col span="12">
<div flex="main:center">
<div class="demo-image-lazy">
<div class="demo-image-lazy-inner">
<b-image v-for="url in hUrlList" :key="url" :src="url" lazy />
</div>
</div>
</div>
<p class="t-center">横向</p>
</b-col>
</b-row>
</template>
<script setup>
const vUrlList = [
'https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg',
'https://fuss10.elemecdn.com/1/34/19aa98b1fcb2781c4fba33d850549jpeg.jpeg',
'https://fuss10.elemecdn.com/0/6f/e35ff375812e6b0020b6b4e8f9583jpeg.jpeg',
'https://fuss10.elemecdn.com/9/bb/e27858e973f5d7d3904835f46abbdjpeg.jpeg',
'https://fuss10.elemecdn.com/d/e6/c4d93a3805b3ce3f323f7974e6f78jpeg.jpeg',
'https://fuss10.elemecdn.com/3/28/bbf893f792f03a54408b3b7a7ebf0jpeg.jpeg',
'https://fuss10.elemecdn.com/2/11/6535bcfb26e4c79b48ddde44f4b6fjpeg.jpeg'
]
const hUrlList = [
'https://file.iviewui.com/images/image-demo-1.jpg',
'https://file.iviewui.com/images/image-demo-2.jpg',
'https://file.iviewui.com/images/image-demo-3.jpg',
'https://file.iviewui.com/images/image-demo-4.jpg',
'https://file.iviewui.com/images/image-demo-5.jpg',
'https://file.iviewui.com/images/image-demo-6.jpg'
]
</script>
<style scoped>
.demo-image-lazy-vertical {
height: 300px;
overflow-y: auto;
.bin-image {
display: block;
min-height: 200px;
}
}
.demo-image-lazy {
width: 400px;
height: 200px;
overflow: hidden;
overflow-x: scroll;
.demo-image-lazy-inner {
width: 3000px;
height: 200px;
}
.bin-image {
float: left;
width: 400px;
height: 200px;
}
}
</style>
<template>
<b-row gutter="20">
<b-col span="12">
<div class="demo-image-lazy-vertical">
<b-image v-for="url in vUrlList" :key="url" :src="url" lazy />
</div>
<p class="t-center">纵向</p>
</b-col>
<b-col span="12">
<div flex="main:center">
<div class="demo-image-lazy">
<div class="demo-image-lazy-inner">
<b-image v-for="url in hUrlList" :key="url" :src="url" lazy />
</div>
</div>
</div>
<p class="t-center">横向</p>
</b-col>
</b-row>
</template>
<script setup>
const vUrlList = [
'https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg',
'https://fuss10.elemecdn.com/1/34/19aa98b1fcb2781c4fba33d850549jpeg.jpeg',
'https://fuss10.elemecdn.com/0/6f/e35ff375812e6b0020b6b4e8f9583jpeg.jpeg',
'https://fuss10.elemecdn.com/9/bb/e27858e973f5d7d3904835f46abbdjpeg.jpeg',
'https://fuss10.elemecdn.com/d/e6/c4d93a3805b3ce3f323f7974e6f78jpeg.jpeg',
'https://fuss10.elemecdn.com/3/28/bbf893f792f03a54408b3b7a7ebf0jpeg.jpeg',
'https://fuss10.elemecdn.com/2/11/6535bcfb26e4c79b48ddde44f4b6fjpeg.jpeg'
]
const hUrlList = [
'https://file.iviewui.com/images/image-demo-1.jpg',
'https://file.iviewui.com/images/image-demo-2.jpg',
'https://file.iviewui.com/images/image-demo-3.jpg',
'https://file.iviewui.com/images/image-demo-4.jpg',
'https://file.iviewui.com/images/image-demo-5.jpg',
'https://file.iviewui.com/images/image-demo-6.jpg'
]
</script>
<style scoped>
.demo-image-lazy-vertical {
height: 300px;
overflow-y: auto;
.bin-image {
display: block;
min-height: 200px;
}
}
.demo-image-lazy {
width: 400px;
height: 200px;
overflow: hidden;
overflow-x: scroll;
.demo-image-lazy-inner {
width: 3000px;
height: 200px;
}
.bin-image {
float: left;
width: 400px;
height: 200px;
}
}
</style>
预览
设置属性 preview
可以开启图片预览模式,通过属性 preview-list
来设置图片列表,initial-index
属性设置打开预览时显示图片的索引。
预览时,可以使用 ←
、→
切换图片,↑
、↓
缩放图片,Space
显示 1:1
图片,ESC
退出预览。
<template>
<b-space>
<template v-for="(url, index) in urlList" :key="url">
<b-image
:src="url"
fit="contain"
width="120px"
height="80px"
preview
:preview-list="urlList"
:initial-index="index"
/>
</template>
</b-space>
</template>
<script setup>
const urlList = [
'https://file.iviewui.com/images/image-demo-1.jpg',
'https://file.iviewui.com/images/image-demo-2.jpg',
'https://file.iviewui.com/images/image-demo-3.jpg',
'https://file.iviewui.com/images/image-demo-4.jpg',
'https://file.iviewui.com/images/image-demo-5.jpg',
'https://file.iviewui.com/images/image-demo-6.jpg'
]
</script>
<template>
<b-space>
<template v-for="(url, index) in urlList" :key="url">
<b-image
:src="url"
fit="contain"
width="120px"
height="80px"
preview
:preview-list="urlList"
:initial-index="index"
/>
</template>
</b-space>
</template>
<script setup>
const urlList = [
'https://file.iviewui.com/images/image-demo-1.jpg',
'https://file.iviewui.com/images/image-demo-2.jpg',
'https://file.iviewui.com/images/image-demo-3.jpg',
'https://file.iviewui.com/images/image-demo-4.jpg',
'https://file.iviewui.com/images/image-demo-5.jpg',
'https://file.iviewui.com/images/image-demo-6.jpg'
]
</script>
单独预览组件
图片预览组件 ImagePreview
也可以单独使用。
<template>
<div>
<b-button @click="handleShowPreview">打开图片预览</b-button>
<b-image-preview v-model="preview" :preview-list="urlList" />
</div>
</template>
<script setup>
import { ref } from 'vue'
const urlList = [
'https://wangbin3162.github.io/bin-files/animals/00.jpg',
'https://wangbin3162.github.io/bin-files/animals/01.jpg',
'https://wangbin3162.github.io/bin-files/animals/02.jpg',
'https://wangbin3162.github.io/bin-files/animals/03.jpg',
'https://wangbin3162.github.io/bin-files/animals/04.jpg',
'https://wangbin3162.github.io/bin-files/animals/05.jpg',
'https://wangbin3162.github.io/bin-files/animals/06.jpg',
'https://wangbin3162.github.io/bin-files/animals/07.jpg'
]
const preview = ref(false)
function handleShowPreview() {
preview.value = true
}
</script>
<template>
<div>
<b-button @click="handleShowPreview">打开图片预览</b-button>
<b-image-preview v-model="preview" :preview-list="urlList" />
</div>
</template>
<script setup>
import { ref } from 'vue'
const urlList = [
'https://wangbin3162.github.io/bin-files/animals/00.jpg',
'https://wangbin3162.github.io/bin-files/animals/01.jpg',
'https://wangbin3162.github.io/bin-files/animals/02.jpg',
'https://wangbin3162.github.io/bin-files/animals/03.jpg',
'https://wangbin3162.github.io/bin-files/animals/04.jpg',
'https://wangbin3162.github.io/bin-files/animals/05.jpg',
'https://wangbin3162.github.io/bin-files/animals/06.jpg',
'https://wangbin3162.github.io/bin-files/animals/07.jpg'
]
const preview = ref(false)
function handleShowPreview() {
preview.value = true
}
</script>
Props
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
src | 图片地址 | String | — | — |
alt | 图片描述 | String | — | — |
referrer-policy | 原生属性 | String | — | — |
width | 宽度 | String Number | — | — |
height | 高度 | String Number | — | — |
fit | 图片适配容器模式包含 fill,contain,cover,none,scale-down | String | — | |
lazy | 是否懒加载 | Boolean | — | false |
scroll-container | 加载容器 | String , HTMLElement | — | false |
append-to-body | 是否将弹层放置于 body 内 | Boolean | — | false |
mask-closable | 是否允许点击遮罩层关闭 | Boolean | — | true |
preview-tip | 是否显示预览提示和遮罩 | Boolean | — | true |
preview | 是否图片预览 | Boolean | — | false |
preview-list | 图片预览列表 | Array | — | [] |
infinite | 是否循环切换 | Boolean | — | true |
initial-index | 打开预览的第一项 | Number | — | 0 |
toolbar | 图片预览操作栏选项,按数组顺序排序 | Array | — | ['zoomIn', 'zoomOut', 'original', 'rotateLeft', 'rotateRight', 'download'] |
initial-index | 打开预览的第一项 | Number | — | 0 |
failText | 失败文字 | String | — | 失败 |
loadingText | 加载文字 | String | — | 加载中 |
previewText | 预览文字 | String | — | 预览 |
Events
事件名称 | 说明 | 返回值 |
---|---|---|
load | 图片加载成功 | - |
error | 图片加载失败 | - |
switch | 图片预览切换 | - |
close | 图片预览关闭 | Object |
click | 图片点击 | initialIndex |
Slots
名称 | 说明 |
---|---|
placeholder | 自定义图片加载中 |
error | 自定义图片加载失败 |
preview | 自定义图片预览 |