viewImage.vue
1.92 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
<template>
<div class="view-image-container">
<el-dialog
:visible.sync="visible"
:width="dialogWidth"
:show-close="true"
custom-class="image-viewer-dialog"
@close="close"
center
>
<div class="image-wrapper">
<img
:src="imageInfo.url"
:style="{
maxWidth: '100%',
maxHeight: imageInfo.maxHeight + 'px'
}"
@error="handleImageError"
/>
</div>
</el-dialog>
</div>
</template>
<script>
export default {
name: 'ViewImage',
data() {
return {
visible: false,
dialogWidth: '50%',
imageInfo: {
url: '',
maxHeight: 600
}
}
},
methods: {
open(params) {
this.imageInfo.url = params.url
this.visible = true
// 根据屏幕尺寸动态调整对话框大小
const screenWidth = window.innerWidth
if (screenWidth < 768) {
this.dialogWidth = '90%'
this.imageInfo.maxHeight = 400
} else if (screenWidth < 1200) {
this.dialogWidth = '70%'
this.imageInfo.maxHeight = 500
} else {
this.dialogWidth = '50%'
this.imageInfo.maxHeight = 600
}
},
close() {
this.visible = false
this.imageInfo = {
url: '',
maxHeight: 600
}
},
handleImageError(e) {
e.target.src = '/img/noPhoto.jpg'
}
}
}
</script>
<style lang="scss" scoped>
.view-image-container {
::v-deep .image-viewer-dialog {
.el-dialog__header {
padding: 10px;
}
.el-dialog__body {
padding: 20px;
text-align: center;
}
.image-wrapper {
display: flex;
justify-content: center;
align-items: center;
min-height: 200px;
img {
display: block;
width: auto;
height: auto;
object-fit: contain;
border-radius: 4px;
}
}
}
}
</style>