ImgUploader.vue
1.87 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
<template>
<div id="ImgUploader">
<div class="imgItem" v-for="(item, index) in displayList" :key="index">
<img :src="item" alt="">
</div>
<div class="imgItem addImg" v-show="imgList.length < limit" @click="addImg">
<v-icon>add</v-icon>
</div>
<input type="file" ref="input" @change="handleChange" :multiple="limit > 1" :accept="accept">
</div>
</template>
<script>
export default {
data: () => ({
imgList: [],
displayList: [],
file: {}
}),
props: {
limit: {
type: Number,
default: 1
},
accept: {
type: String,
default: 'image/gif, image/png, image/jpeg, image/bmp, image/webp'
}
},
methods: {
addImg() {
this.$refs.input.click()
},
handleChange(e) {
const files = e.target.files;
for (let i = 0; i < files.length; i++) {
if (this.imgList.length < this.limit) {
const file = files[i]
let fr = new FileReader()
fr.readAsDataURL(file)
this.imgList.push(file)
fr.onloadend = (e) => {
this.displayList.push(e.target.result)
}
}
}
console.log(this.imgList)
},
}
}
</script>
<style lang="scss" scoped>
#ImgUploader {
width: 100%;
display: flex;
justify-content: flex-start;
align-items: center;
flex-direction: row;
flex-wrap: wrap;
.imgItem {
width: 5rem;
height: 5rem;
margin-right: 0.5rem;
margin-top: 0.5rem;
transition: all 0.3s linear;
img {
width: auto;
height: auto;
max-width: 100%;
max-height: 100%;
vertical-align: bottom;
}
.icon {
font-size: 4rem;
}
&.addImg {
border: 1px dotted #979797;
line-height: 5rem;
text-align: center;
}
}
input {
height: 0;
width: 0;
}
}
</style>