跳到内容
SCRM DOC
Vue前端Lottie创建于2024-04-26更新于 2025-03-22

Vue3-lottie 动画的使用

Vue

安装

yarn add vue3-lottie@latest
// or
npm install vue3-lottie@latest --save

全局注册

main.js

import { createApp } from 'vue'
import Vue3Lottie from 'vue3-lottie'
import 'vue3-lottie/dist/style.css'

const app = createApp(App)
app.use(Vue3Lottie).mount('#app')

// 1.重命名组件
// app.use(Vue3Lottie, { name: 'LottieAnimation' }) 
// 2.使用
// <LottieAnimation />

components.d.ts

// 要为Volar类型检查定义全局组件,需要添加:
declare module '@vue/runtime-core' {
  export interface GlobalComponents {
    LottieAnimation: typeof import('vue3-lottie')['Vue3Lottie']
  }
}

declare module '*.json' {
  const value: any;
  export default value;
}

export {}

使用组件

<template>
  <Vue3Lottie :animationData="DogJSON" :height="200" :width="200" />
</template>
 
<script setup>
import DogJSON from './lotties/dog.json'
</script>

相关配置

Prop类型默认值描述
animationDataObject{}作为 JSON 对象提供的 lottie 动画数据
animationLinkString''Lottie 动画数据的 URL 链接
widthNumber or String"100%"lottie 动画容器的宽度 (数字对应像素值)
heightNumber or String"100%"lottie 动画容器的高度 (数字对应像素值)
speedNumber"1"lottie 动画的速度
directionString"forward"动画播放方向
loopNumber or Booleantruelottie 动画应该运行的实例数 (true 为无限)
autoPlayBooleantrue在组件加载时启动动画
delayNumber0延迟动画播放状态几毫秒
pauseOnHoverBooleanfalse是否在悬停时暂停动画
playOnHoverBooleanfalse当鼠标悬停时是否播放动画
backgroundColorStringtransparent容器的背景颜色
rendererString"svg"设置渲染器

本页目录