当前位置:网站首页>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" />
下面是改造后的效果
边栏推荐
- Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
- 简单排序(暑假每日一题 14)
- html select标签赋值数据库查询结果
- 静态/动态代理模式
- 持续投入商品研发,叮咚买菜赢在了供应链投入上
- 一个注解替换synchronized关键字:分布式场景下实现方法加锁
- 2022年茶艺师(中级)考试试题模拟考试平台操作
- initramfs详解----添加硬盘驱动并访问磁盘
- Flask框架初学-05-命令管理Manager及数据库的使用
- 【store商城项目01】环境准备以及测试
猜你喜欢

esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)

nodejs切换版本使用(不需要卸载重装)

initramfs详解----添加硬盘驱动并访问磁盘

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

5.scrapy中间件&分布式爬虫

nodejs+express realizes the access to the database mysql and displays the data on the page

Example 041: Methods and variables of a class

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

Snake game bug analysis and function expansion

贪吃蛇游戏Bug解析及功能扩展
随机推荐
mpf5_定价Bond_yield curve_Spot coupon_duration_有效利率_连续复利_远期_Vasicek短期_CIR模型Derivatives_Tridiagonal_ppf
简单排序(暑假每日一题 14)
香港服务器有哪些常用的型号
html select tag assignment database query result
Example 039: Inserting elements into an ordered list
- heavy OpenCV 】 【 mapping
JS 从零教你手写节流throttle
nodejs切换版本使用(不需要卸载重装)
flask框架初学-06-对数据库的增删改查
Small Turtle Compilation Notes
2022焊工(初级)上岗证题目模拟考试平台操作
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?
cdh6.x 集成spark-sql
Continuing to pour money into commodities research and development, the ding-dong buy vegetables in win into the supply chain
内网穿透-应用
initramfs详解----添加硬盘驱动并访问磁盘
关联接口测试
持续投入商品研发,叮咚买菜赢在了供应链投入上
如何通过API接口从淘宝(或天猫店)复制宝贝到拼多多接口代码对接教程
安全至上:落地DevSecOps最佳实践你不得不知道的工具