vue3通过lottie-web插件使用json动画

使用lottie-web插件来实现json动画

安装:yarn add lottie-web

在utils目录(自己创建的)下,创建common.ts(或者common.js),内容如下:

import lottie from 'lottie-web'

export const jsonAnimation = (dom: string = '', jsonFile: any) => {
  lottie.loadAnimation({
    container: document.querySelector(dom) as Element,
    renderer: 'svg',
    loop: true,
    autoplay: true,
    animationData: jsonFile
  })
}

container:表示要挂载的dom元素

loop:表示循环

autoplay:字如其意,自动播放

animationData:使用的json文件

更多配置:https://github.com/airbnb/lottie-web

使用起来就简单了

<script setup lang="ts">
import { onMounted } from 'vue'
import btnJson from '@/assets/animation/login-btn.json'
import { jsonAnimation } from '@/utils/common'

const onLogin = () => {}

onMounted(() => {
  jsonAnimation('#login-btn', btnJson)
})
</script>

<template>
  <div class="login">
    <div id="login-btn" class="login-btn" @click="onLogin"></div>
  </div>
</template>

<style lang="less" scoped>
.login {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-btn {
    width: 228px;
    height: 50px;
    cursor: pointer;
  }
}
</style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3使用Lottie-web相对简单。首先,你需要确保已经安装了Lottie-web库,你可以通过npm或yarn来安装它。一旦安装完成,你就可以在Vue 3项目使用它了。 在你的Vue 3项目,首先需要在你的组件导入Lottie-web库。你可以在组件的头部添加以下代码来导入Lottie-web: import lottie from 'lottie-web'; 接下来,你可以在组件的methods或mounted函数使用Lottie-web来加载和播放动画。例如,你可以在mounted函数添加以下代码: mounted() { this.loadAnimation(); }, methods: { loadAnimation() { const element = document.getElementById('lottie-animation'); if (element) { const animation = lottie.loadAnimation({ container: element, renderer: 'svg', loop: true, autoplay: true, path: 'path/to/your/animation.json' // 你的动画文件的路径 }); } } } 在上面的代码,我们首先通过getElementById获取到一个DOM元素,它将用作动画的容器。然后,我们使用Lottie-web的loadAnimation函数来加载动画,传递了一些选项参数,比如renderer,loop和autoplay。最后,我们指定了动画文件的路径。 这只是一个简单的例子,你可以根据你的需求来调整代码。请确保在你的组件模板有一个对应的DOM元素,它将被用作动画的容器,例如: <template> <div id="lottie-animation"></div> </template> 这样你就可以在Vue 3使用Lottie-web了。记得根据你的具体需求来进行相应的配置和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值