当前位置:网站首页>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" />
下面是改造后的效果
边栏推荐
- HBuilderX的下载安装和创建/运行项目
- lombok注解@RequiredArgsConstructor的使用
- 小程序:扫码打开参数解析
- 持续投入商品研发,叮咚买菜赢在了供应链投入上
- sudo 权限控制,简易
- html select标签赋值数据库查询结果
- Quickly build a website with static files
- Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
- priority_queue元素为指针时,重载运算符失效
- 【云原生】DevOps(六):Jenkins流水线
猜你喜欢

Flask框架初学-05-命令管理Manager及数据库的使用

Snake game bug analysis and function expansion

计算首屏时间

实例038:矩阵对角线之和

ssh服务详解

Use nodejs switch version (no need to uninstall and reinstall)

Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment

Qt中对象树的机制介绍以及底层实现,各种结果分析:(以及自己写容易犯错的点)

工程制图平面投影练习

小甲鱼汇编笔记
随机推荐
GNSS[0]- Topic
2022焊工(初级)上岗证题目模拟考试平台操作
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
DDTL: Domain Transfer Learning at a Distance
Multithreading JUC Learning Chapter 1 Steps to Create Multithreading
实例040:逆序列表
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
Example: 036 is a prime number
v-model
一篇文章看懂JS闭包,从执行上下文角度解析有趣的闭包
Web APIs BOM- 操作浏览器:swiper 插件
C程序编译和预定义详解
Engineering drawing review questions (with answers)
nodejs install multi-version version switching
lombok注解@RequiredArgsConstructor的使用
Example 041: Methods and variables of a class
【QT小记】QT中信号和槽的基本使用
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
GNSS文章汇总
APP电商如何快速分润分账?