当前位置:网站首页>web端动效 lottie-web 使用
web端动效 lottie-web 使用
2022-08-04 01:58:00 【ZionHH】
Lottie 是 Airbnb 开发的,一个适用于 Android、iOS、Web 和 Windows 的库,能够渲染 Adobe After Effects 动画特效。
- 安装
yarn add lottie-web
- 使用方法
我这里创建了一个组件MyLottie.vue
<!-- components/MyLottie -->
<template>
<!-- 渲染容器 -->
<div class="lottie"></div>
</template>
<script> import lottie from 'lottie-web' export default {
props: {
animationData: {
type: Object, required: true }, autoplay: {
type: Boolean, default: true }, loop: {
type: Boolean, default: true } }, data () {
return {
lottie: null } }, mounted () {
this.intLottie() }, methods: {
intLottie () {
const {
animationData, autoplay, loop } = this this.lottie = lottie.loadAnimation({
container: this.$el, // 渲染容器 renderer: 'svg', // 渲染方式 svg|canvas|html loop, // 是否循环 true|false|number autoplay, // 自动播放 true|false animationData // lottie json文件 }) } } } </script>
<style lang="scss" scoped> .lottie {
width: 200px; height: 200px; } </style>
在页面引用组件,可以去官方社区下载动画json
<template>
<div>
<MyLottie :animationData="lottieJson" />
</div>
</template>
<script> import MyLottie from '@/components/MyLottie' export default {
name: 'Home', components: {
MyLottie }, data () {
return {
// lottie json文件 lottieJson: require('@/assets/lottie/lottie.json') } } } </script>
这样一个lottie动画就出来了
下面结合lottie的一些实例方法,将我们的lottie组件添加鼠标事件,官方文档
html部分修改
<div class="lottie" @mouseenter="onMouseenter" @mouseleave="onMouseleave"></div>
methods部分修改
methods: {
intLottie () {
let {
animationData, autoplay, loop } = this
if (!autoplay) {
// 这里交互播放不循环
loop = false
}
this.lottie = lottie.loadAnimation({
container: this.$el, // 渲染容器
renderer: 'svg', // 渲染方式
loop, // 是否循环
autoplay, // 自动播放
animationData // lottie json文件
})
},
onMouseenter () {
// 我这里demo,非自动播放则判断为鼠标交互播放,具体封装根据自身业务场景来
if (!this.autoplay) {
this.lottie.setDirection(1)
this.lottie.play()
// 监听lottie播放结束
this.lottie.addEventListener('complete', e => {
this.lottie.stop()
})
}
},
onMouseleave () {
this.lottie.removeEventListener('complete')
if (!this.autoplay) {
// 改变播放方向,鼠标移出回退
this.lottie.setDirection(-1)
}
}
}
}
引入组件处修改
<MyLottie :animationData="lottie" :autoplay="false" />
下面是改造后的效果
边栏推荐
- 企业虚拟偶像产生了实质性的价值效益
- 简单排序(暑假每日一题 14)
- Summary of GNSS Articles
- LDO investigation
- Example 041: Methods and variables of a class
- Array_Sliding window | leecode brushing notes
- C program compilation and predefined detailed explanation
- esp32发布机器人电池电压到ros2(micro-ros+CoCube)
- 工程制图名词解释-重点知识
- Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
猜你喜欢

小程序:扫码打开参数解析

C程序编译和预定义详解

TensoFlow学习记录(二):基础操作
通用的测试用例编写大全(登录测试/web测试等)

- heavy OpenCV 】 【 mapping
![Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.](/img/10/87c0bedd49b5dce6fbcd28ac361145.png)
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.

SAP SD module foreground operation

HBuilderX的下载安装和创建/运行项目

lombok注解@RequiredArgsConstructor的使用

云开发旅游打卡广场微信小程序源码(含视频教程)
随机推荐
【云原生】DevOps(六):Jenkins流水线
Slipper - virtual point, shortest path
this巩固训练,从两道执行题加深理解闭包与箭头函数中的this
织梦内核电动伸缩门卷闸门门业公司网站模板 带手机版【站长亲测】
实例040:逆序列表
HBuilderX的下载安装和创建/运行项目
实例035:设置输出颜色
sudo 权限控制,简易
Snake game bug analysis and function expansion
持续投入商品研发,叮咚买菜赢在了供应链投入上
小甲鱼汇编笔记
Parquet encoding
priority_queue元素为指针时,重载运算符失效
Presto中broadcast join和partition join执行计划的处理过程
SAP SD module foreground operation
What is SVN (Subversion)?
Example: 036 is a prime number
工程制图名词解释-重点知识
内网穿透-应用
22/8/3(板子)树状dp板子+中国剩余定理+求组合数3,4+容斥原理