viewImage.vue
1.79 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
<template>
<div class="view-image-container">
<el-dialog
:visible.sync="dialogVisible"
fullscreen
:show-close="false"
custom-class="image-viewer-dialog"
>
<div class="image-wrapper">
<img
:src="imageUrl"
:style="imageStyle"
@error="handleImageError"
/>
<i
class="el-icon-close close-icon"
@click="close"
/>
</div>
</el-dialog>
</div>
</template>
<script>
export default {
name: 'ViewImage',
data() {
return {
dialogVisible: false,
imageUrl: '',
imageStyle: {
width: '800px',
height: 'auto',
display: 'block',
margin: '0 auto'
}
}
},
methods: {
open(url) {
this.imageUrl = url
this.dialogVisible = true
// 预加载图片获取实际尺寸
const img = new Image()
img.src = url
img.onload = () => {
const imgScale = img.width / img.height
this.imageStyle.width = '800px'
this.imageStyle.height = `${800 / imgScale}px`
}
},
close() {
this.dialogVisible = false
this.imageUrl = ''
},
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;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
.close-icon {
position: absolute;
top: 20px;
right: 20px;
font-size: 24px;
color: #fff;
cursor: pointer;
z-index: 2001;
&:hover {
color: #f56c6c;
}
}
}
}
}
</style>