LottieAnimation.vue 952 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <template>
  2. <div ref="lottieContainer" class="lottie-container"></div>
  3. </template>
  4. <script>
  5. import lottie from 'lottie-web';
  6. export default {
  7. props: {
  8. animationData: {
  9. type: Object,
  10. required: true
  11. },
  12. loop: {
  13. type: Boolean,
  14. default: true
  15. },
  16. autoplay: {
  17. type: Boolean,
  18. default: true
  19. }
  20. },
  21. mounted() {
  22. this.initLottie();
  23. },
  24. beforeDestroy() {
  25. this.destroyLottie();
  26. },
  27. methods: {
  28. initLottie() {
  29. this.animation = lottie.loadAnimation({
  30. container: this.$refs.lottieContainer, // DOM元素
  31. renderer: 'svg',
  32. loop: this.loop,
  33. autoplay: this.autoplay,
  34. animationData: this.animationData // 动画数据
  35. });
  36. },
  37. destroyLottie() {
  38. if (this.animation) {
  39. this.animation.destroy(); // 销毁动画
  40. }
  41. }
  42. }
  43. };
  44. </script>
  45. <style scoped>
  46. .lottie-container {
  47. width: 100%;
  48. height: 100%;
  49. }
  50. </style>