viewImage.vue
1.3 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
<template>
<el-dialog
:visible.sync="visible"
fullscreen
:show-close="false"
@close="handleClose"
>
<div style="position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%);">
<el-image
:src="imageUrl"
:style="imageStyle"
fit="contain"
/>
<i
class="el-icon-close"
style="position: absolute; right: 20px; top: 20px; font-size: 24px; color: red; cursor: pointer;"
@click="visible = false"
/>
</div>
</el-dialog>
</template>
<script>
export default {
name: 'ViewImage',
data() {
return {
visible: false,
imageUrl: '',
imageStyle: {
width: '800px',
height: '800px'
}
}
},
methods: {
open(url) {
this.imageUrl = url
this.visible = true
this.adjustImageSize(url)
},
handleClose() {
this.imageUrl = ''
},
adjustImageSize(url) {
const img = new Image()
img.src = url
img.onload = () => {
const ratio = img.width / img.height
if (ratio > 1) {
this.imageStyle.height = `${800 / ratio}px`
this.imageStyle.width = '800px'
} else {
this.imageStyle.width = `${800 * ratio}px`
this.imageStyle.height = '800px'
}
}
}
}
}
</script>