vue3 中 lottie-web 封装组件

在这里插入图片描述




用到的JSON文件在“我的资源”里,下面这个链接直达

下面的代码中用到的JSON数据源

Lottie.vue

<script setup>
import { ref, onMounted } from 'vue'
import lottie from 'lottie-web'

// 设置组件参数
const props = defineProps({
  renderer: {
    type: String,
    default: 'svg',
  },
  // 循环播放
  loop: {
    type: Boolean,
    default: true,
  },
  autoplay: {
    type: Boolean,
    default: true,
  },
  animationData: {
    type: Object,
    default: () => ({}),
  },
  name: {
    type: String,
    default: '',
  },
})

// 创建 lottie 接收变量和获取dom
const animation = ref(null)
const dom = ref(null)

// 创建事件返回初始化lottie对象
const emits = defineEmits(['getAnimation', 'getDom'])

// 初始化渲染 lottie动画,并返回lottie对象
onMounted(() => {
  animation.value = lottie.loadAnimation({
    container: dom.value, // 用于渲染的容器
    // 渲染方式 svg、canvas、html
    renderer: props.renderer,
    // 是否循环
    loop: props.loop,
    autoplay: props.autoplay, // 自动播放
    // UED 提供的 动画的 json 文件
    animationData: props.animationData.default,
    name: props.name,
  })
  emits('getAnimation', animation.value)
})
</script>

<template>
  <!-- 渲染 lottie 动画 -->
  <div id="lottieId" ref="dom"></div>
</template>

<style scoped>
#lottieId {
  width: 100%;
  height: 100%;
}
</style>

在别的组件使用它


import Lottie from './components/Lottle.vue'

<Lottie :animation-data="Animation_1" />
 

相关推荐

  1. Vue 3使用 Lottie 动画

    2024-07-17 16:52:02       27 阅读
  2. vite+vue3项目svg图标组件封装

    2024-07-17 16:52:02       39 阅读
  3. vue3实现地区下拉选择组件封装

    2024-07-17 16:52:02       26 阅读
  4. vue3常用组件封装及使用

    2024-07-17 16:52:02       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 16:52:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 16:52:02       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 16:52:02       58 阅读
  4. Python语言-面向对象

    2024-07-17 16:52:02       69 阅读

热门阅读

  1. 【14】水仙花数

    2024-07-17 16:52:02       19 阅读
  2. vue3项目,管控部分路由仅管理员可见

    2024-07-17 16:52:02       19 阅读
  3. 乡下人的悲歌书籍pdf下载

    2024-07-17 16:52:02       23 阅读
  4. ES6基本语法(二)——函数与数组

    2024-07-17 16:52:02       20 阅读
  5. Jupyter Notebook 一些常用的快捷键

    2024-07-17 16:52:02       19 阅读
  6. linux 修改hostname

    2024-07-17 16:52:02       23 阅读
  7. 【Oracle】Oracle语法之递归查询

    2024-07-17 16:52:02       19 阅读
  8. C++基础练习 - Chapter 3

    2024-07-17 16:52:02       17 阅读