-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lottie.vue
66 lines (61 loc) · 1.12 KB
/
Lottie.vue
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
<script setup lang="ts">
import lottie from 'lottie-web'
const props = defineProps({
width: {
type: String,
default: '100px',
},
height: {
type: String,
default: '100px',
},
src: {
type: String,
default: 'https://assets10.lottiefiles.com/packages/lf20_dyiqnus5.json',
},
jsonData: {
type: Object,
default: () => null,
},
autoplay: {
type: Boolean,
default: true,
},
loop: {
type: Boolean,
default: false,
},
})
const animation = ref(null)
onMounted(() => {
initAnim()
})
function initAnim(data?: Record<string, any>) {
if (animation.value) {
lottie.loadAnimation({
container: animation.value,
renderer: 'svg',
loop: props.loop,
autoplay: props.autoplay,
path: props.src,
// animationData只能加载本地json,优先级高于path
animationData: data || props.jsonData,
})
}
}
// 重新加载动画
function reloadAnim(data?: Record<string, any>) {
if (animation.value) {
lottie.destroy()
initAnim(data)
}
}
// 暴露给外部的方法
defineExpose({
reloadAnim,
})
</script>
<template>
<div ref="animation" :style="{ width, height }" />
</template>
<style scoped></style>