组件:
<template>
<div class="content" :style="[{ width: width, height: height, borderRadius: radius }]">
<div
class="status-1"
v-if="is_Loading"
:class="[shape ? 'img-border-radius' : '']"
:style="[{ width: '100%', height: '100%' }]"
>
<a-icon v-if="is_error" type="exclamation-circle" />
<a-icon v-else-if="is_Loading" type="loading" />
<div v-if="is_error" class="trip">加载失败</div>
</div>
<img
v-if="!is_error"
:src="src"
alt="暂无图片"
@click="viewImg"
class="imgs"
:class="[shape ? 'img-border-radius' : '', mode, hover ? 'is-hover' : '']"
:style="[{ width: '100%', height: '100%' }]"
@error="imgOnRrror"
@load="imgOnLoad"
:title="visible ? '点击查看图片' : ''"
/>
<a-modal v-model="visibleImg" title="查看图片" :footer="null" :width="modalWidth" centered>
<img :src="src" alt="暂无图片" :style="[{ width: '100%', height: '100%' }]" @error="imgOnRrror" />
</a-modal>
</div>
</template>
<script>
export default {
name: 'BeautifulImage',
props: {
src: {
type: String,
default: () => '',
},
width: {
type: String,
default: () => '100%',
},
height: {
type: String,
default: () => '100%',
},
//是否为圆形
shape: {
type: Boolean,
default: () => false,
},
//圆角
radius: {
type: String,
default: () => '0px',
},
//图片显示方式 contain cover fill scale-down
mode: {
type: String,
default: () => '',
},
//是否开启点击图片查看功能
visible: {
type: Boolean,
default: () => false,
},
//是否鼠标经过动画效果
hover: {
type: Boolean,
default: () => false,
},
modalWidth: {
type: String,
default: () => '50%',
},
},
data() {
return {
visibleImg: false,
defaultImg: '', //默认图片
is_error: false, //是否加载失败
is_Loading: true, //false为加载完成
}
},
created() {},
methods: {
viewImg() {
if (this.visible) {
this.visibleImg = true
}
},
imgOnLoad(e) {
this.is_error = false
this.is_Loading = false
},
imgOnRrror(e) {
this.is_error = true
},
},
}
</script>
<style lang="less" scoped>
.content {
cursor: pointer;
overflow: hidden;
}
.imgs {
transition: all 0.3s;
}
.contain {
object-fit: contain;
}
.cover {
object-fit: cover;
}
.fill {
object-fit: fill;
}
.none {
object-fit: none;
}
.scale-down {
object-fit: scale-down;
}
.img-border-radius {
border-radius: 50% !important;
}
.is-hover:hover {
transform: scale(1.2);
}
.status-1 {
background-color: #f3f4f6;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
.trip{
font-size: 10px;
margin-top: 2px;
}
}
</style>
使用方法:
<beautiful-image
:src="record.img"
width="60px"
height="60px"
visible
hover
radius="6px"
modalWidth="56%"
/>
效果图:
