img-format.vue
1.2 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
<template>
<img v-if="lazy" v-lazy="currentSrc" alt="" :width="width" :height="height">
<img v-else ref="image" :src="currentSrc" alt="" :width="width" :height="height">
</template>
<script>
import {getImgUrl} from 'common/utils';
export default {
name: 'ImgFormat',
props: {
src: String,
w: [Number, String],
h: [Number, String],
width: [Number, String],
height: [Number, String],
lazy: Boolean,
mode: {
type: Number,
default: 2
}
},
computed: {
currentSrc() {
if (this.w > 0 && this.h > 0) {
return getImgUrl(this.src, this.w, this.h, this.mode);
}
return this.src;
}
},
mounted() {
let timer;
let img = new Image();
let maxRetry = 30 * 1000;
let start = Date.now();
timer = setInterval(() => {
img.src = this.$refs.image.src;
if (!img.src) {
clearInterval(timer);
}
if (img.complete || Date.now() - start > maxRetry) {
clearInterval(timer);
}
}, 3000);
}
};
</script>
<style>
</style>