Image 图片
基础用法
可以使用fit
属性来指定如何调整图片尺寸,src
属性来指定图片的来源。使用alt
属性来指定图片的替代文本。
fill
加载中...
contain
加载中...
cover
加载中...
none
加载中...
scale-down
加载中...
<template>
<div class="container">
<div class="item">
<h4>fill</h4>
<ym-image :src="src" fit="fill" style="width:100px;height:100px"></ym-image>
</div>
<div class="item">
<h4>contain</h4>
<ym-image :src="src" fit="contain" style="width:100px;height:100px"></ym-image>
</div>
<div class="item">
<h4>cover</h4>
<ym-image :src="src" fit="cover" style="width:100px;height:100px"></ym-image>
</div>
<div class="item">
<h4>none</h4>
<ym-image :src="src" fit="none" style="width:100px;height:100px"></ym-image>
</div>
<div class="item">
<h4>scale-down</h4>
<ym-image :src="src" fit="scale-down" style="width:100px;height:100px"></ym-image>
</div>
</div>
</template>
<script setup lang="ts">
const src = "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
</script>
<style scoped>
.container {
display: flex;
flex-wrap: wrap;
}
.item {
margin: 10px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: #999;
}
</style>
占位内容
可以使用placeholder
属性来指定图片加载时的占位内容。可以使用error
属性来指定图片加载失败时的占位内容。
loading
error
加载中...
<template>
<div class="container">
<div class="item">
<h4>loading</h4>
<ym-image :src="src" fit="cover" style="width:250px;height:120px">
<template #loading>
<ym-icon icon="spinner" spin ></ym-icon>
</template>
</ym-image>
</div>
<div class="item">
<h4>error</h4>
<ym-image :src="src2" style="width:250px;height:120px">
<template #error>
<ym-icon icon="exclamation-triangle" size="2x" ></ym-icon>
</template>
</ym-image>
</div>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const src = ref("")
const src1 = "https://jeek-space-blog.top:3000/images/article/6c372c58e9f76a1059cec97dae6b84d7.jpeg"
const src2 = "https://haowallpaper.com/link/common/file/getCroppingIdsmg/15424390782292288"
setTimeout(() => {
src.value = src1
},2000)
</script>
<style scoped>
.container {
display: flex;
flex-wrap: wrap;
}
.item {
margin: 10px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: #999;
}
</style>
懒加载
可以使用lazy
属性来指定图片是否懒加载。懒加载的图片会在滚动到可视区域时加载。默认可视区域是整个视口,可以通过scrollContainer
属性来指定滚动容器,该属性可以是一个DOM元素或者是一个选择器字符串。 同时也可以使用loading
属性来指定图片加载的模式,与原生的loading
属性相同。
加载中...
加载中...
加载中...
加载中...
加载中...
<template>
<div class="container lazy" ref="containerRef">
<div class="item" v-for="(item,index) in srcList" :key="item">
<ym-image @load="load(index)" :src="item" fit="contain" lazy :scrollContainer="containerRef" style="height: 400px;"></ym-image>
</div>
</div>
</template>
<script setup lang="ts">
import { ref , onMounted } from 'vue'
import { YmMessage } from 'guyan-ym-ui';
const containerRef = ref<any>()
const srcList = [
"https://jeek-space-blog.top:3000/images/article/ab93e2fb1e216541d1aeb751600909f2.jpeg",
"https://jeek-space-blog.top:3000/images/article/d1f2ec4df6a8ca406690312ca69b8d00.jpeg",
"https://jeek-space-blog.top:3000/images/article/43b9c7996880bc8e02858997790e15f7.jpeg",
"https://jeek-space-blog.top:3000/images/article/ea6e433dd3c1e06527b6ac05e76d1677.jpeg",
"https://jeek-space-blog.top:3000/images/article/6c372c58e9f76a1059cec97dae6b84d7.jpeg"
]
onMounted(() => {
console.log(containerRef.value)
})
const load = (index: any) => {
YmMessage.success(`第${index + 1}张图片加载完成`)
}
</script>
<style scoped>
.container {
display: flex;
flex-direction: column;
width: 100%;
height: 400px;
overflow-y: scroll;
gap: 10px;
}
.item {
width: 100%;
}
</style>
图片预览
可以使用previewSrcList
属性来指定预览的图片列表。使用initialIndex
指定预览的初始索引。使用closeOnClickModal
属性来指定是否点击遮罩层关闭预览。
TIP
注意:图片预览效果使用了image-viewer
组件,其他具体用法可参考image-viewer
组件文档。
加载中...
加载中...
加载中...
加载中...
加载中...
加载中...
<template>
<div class="container lazy" >
<div class="item" v-for="(item,index) in srcList" :key="item">
<ym-image preview :src="item" fit="contain" :initialIndex="index" :previewSrcList="srcList"></ym-image>
</div>
</div>
</template>
<script setup lang="ts">
import { ref , onMounted } from 'vue'
const containerRef = ref<any>()
const srcList = [
"https://jeek-space-blog.top:3000/images/article/ab93e2fb1e216541d1aeb751600909f2.jpeg",
"https://jeek-space-blog.top:3000/images/article/d1f2ec4df6a8ca406690312ca69b8d00.jpeg",
"https://jeek-space-blog.top:3000/images/article/43b9c7996880bc8e02858997790e15f7.jpeg",
"https://jeek-space-blog.top:3000/images/article/ea6e433dd3c1e06527b6ac05e76d1677.jpeg",
"https://jeek-space-blog.top:3000/images/article/6c372c58e9f76a1059cec97dae6b84d7.jpeg",
"https://jeek-space-blog.top:3000/images/article/94ba620bf0f1e5b65e5f4889a88d8fba.png"
]
onMounted(() => {
console.log(containerRef.value)
})
</script>
<style scoped>
.container {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
gap: 10px;
}
.item {
width: 30%;
}
</style>
Image API
Props
Name | Description | Type | Default |
---|---|---|---|
src | 图片地址 | string | (必须) |
fit | 图片填充方式 | enum -'fill' | 'contain' | 'cover' | 'none' | 'scale-down' | fill |
alt | 图片的替代文本 | string | - |
lazy | 是否懒加载 | boolean | false |
scrollContainer | 滚动容器 | string | HTMLElement | - |
loading | 图片加载的模式 | string | - |
previewSrcList | 预览的图片列表 | string[] | - |
initialIndex | 预览的初始索引 | number | 0 |
closeOnClickModal | 是否点击遮罩层关闭预览 | boolean | false |
closeOnPressEscape | 是否按下ESC键关闭预览 | boolean | false |
zoomRate | 缩放比例 | number | 1.1 |
minScale | 最小缩放比例 | number | 0.1 |
maxScale | 最大缩放比例 | number | 3 |
Events
Name | Description | Type |
---|---|---|
load | 图片加载完成时触发 | (e: Event) => void |
error | 图片加载失败时触发 | (e: Event) => void |
close | 预览关闭时触发 | (e: Event) => void |
open | 预览打开时触发 | (e: Event) => void |
switch | 预览图片切换时触发 | (e: Event) => void |
Slots
Name | Description | Sub Component |
---|---|---|
error | 加载失败的内容 | - |
loading | 加载中的内容 | - |