viewImage.vue
1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<template>
<div class="view-image-container">
<el-dialog
:visible.sync="visible"
:fullscreen="true"
:show-close="true"
custom-class="image-viewer-dialog"
>
<div class="image-wrapper">
<img
:src="imageInfo.url"
:style="{
width: imageInfo.width + 'px',
height: imageInfo.height + 'px'
}"
@error="handleImageError"
/>
<i class="el-icon-close close-icon" @click="close"></i>
</div>
</el-dialog>
</div>
</template>
<script>
export default {
name: 'ViewImage',
data() {
return {
visible: false,
imageInfo: {
url: '',
width: 800,
height: 800
}
}
},
methods: {
open(params) {
this.imageInfo.url = params.url
this.visible = true
// 动态计算图片尺寸
const img = new Image()
img.src = params.url
img.onload = () => {
const imgScale = img.width / img.height
this.imageInfo.width = 800
this.imageInfo.height = 800 / imgScale
}
},
close() {
this.visible = false
this.imageInfo = {
url: '',
width: 800,
height: 800
}
},
handleImageError(e) {
e.target.src = '/img/noPhoto.jpg'
}
}
}
</script>
<style lang="scss" scoped>
.view-image-container {
.image-viewer-dialog {
background-color: rgba(0, 0, 0, 0.8);
.image-wrapper {
position: relative;
display: flex;
justify-content: center;
align-items: center;
height: 100%;
img {
max-width: 90%;
max-height: 90%;
object-fit: contain;
}
.close-icon {
position: absolute;
top: 20px;
right: 20px;
font-size: 24px;
color: #fff;
cursor: pointer;
z-index: 2001;
&:hover {
color: #f56c6c;
}
}
}
}
}
</style>