widget-lottie.vue
1.05 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>
<div :style="style" ref="lottieContainer"></div>
</template>
<script>
export default {
name: 'WidgetLottie',
props: {
options: {
type: Object,
required: true
}
},
data () {
return {
style: {
width: '100%',
height: '100%',
overflow: 'hidden',
margin: '0 auto'
}
}
},
methods: {
init() {
if (this._anim || !this.options.animationData) {
return Promise.resolve();
}
return import('lottie-web').then(({default: lottie}) => {
this._anim = lottie.loadAnimation({
container: this.$refs.lottieContainer,
renderer: 'svg',
loop: this.options.loop,
autoplay: false,
animationData: this.options.animationData,
});
});
},
play() {
if (this._anim) {
this._anim.play();
} else {
this.init().then(() => {
this._anim && this._anim.play();
});
}
},
stop() {
this._anim && this._anim.stop();
}
}
};
</script>