当前位置:网站首页>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" />
下面是改造后的效果
边栏推荐
- Installation and configuration of nodejs+npm
- 2022年T电梯修理考题及答案
- boot issue
- Example: 036 is a prime number
- 云开发校园微社区微信小程序源码/二手交易/兼职交友微信小程序开源源码
- Kubernetes:(十一)KubeSphere的介绍和安装(华丽的篇章)
- sql有关问题,小时粒度,找到前一个小时内的数据
- 2022 中国算力大会发布“创新先锋”优秀成果
- Presto中broadcast join和partition join执行计划的处理过程
- nodejs+express realizes the access to the database mysql and displays the data on the page
猜你喜欢
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?
C program compilation and predefined detailed explanation
小甲鱼汇编笔记
DDTL: Domain Transfer Learning at a Distance
Priority_queue element as a pointer, the overloaded operators
织梦响应式酒店民宿住宿类网站织梦模板(自适应手机端)
ssh服务详解
lombok注解@RequiredArgsConstructor的使用
Android interview questions and answer analysis of major factories in the first half of 2022 (continuously updated...)
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
随机推荐
【Untitled】
可变字符串
Security First: Tools You Need to Know to Implement DevSecOps Best Practices
LDO investigation
通用的测试用例编写大全(登录测试/web测试等)
nodejs安装及环境配置
如何通过API接口从淘宝(或天猫店)复制宝贝到拼多多接口代码对接教程
Example: 036 is a prime number
【云原生】DevOps(六):Jenkins流水线
第13章 网络安全漏洞防护技术原理与应用
Qt中对象树的机制介绍以及底层实现,各种结果分析:(以及自己写容易犯错的点)
Flutter3.0线程——四步教你如何全方位了解(事件队列)
Array_Sliding window | leecode brushing notes
priority_queue元素为指针时,重载运算符失效
Slipper - virtual point, shortest path
香港服务器有哪些常用的型号
(cf)Codeforces Round #811 (Div. 3)A--E详细题解
Example 041: Methods and variables of a class
2022广东省安全员A证第三批(主要负责人)考试题库及模拟考试
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.