Vue3 中 Lottie動(dòng)畫庫(kù)的使用指南
Lottie 是 Airbnb 開源的一款跨平臺(tái)動(dòng)畫渲染庫(kù),能夠?qū)?AE(After Effects)制作的動(dòng)畫導(dǎo)出為 JSON 格式,并在 Web、iOS、Android 等平臺(tái)無縫渲染,完美還原設(shè)計(jì)師的動(dòng)畫效果。在 Vue3 項(xiàng)目中集成 Lottie,既能提升頁面交互體驗(yàn),又能避免傳統(tǒng) GIF / 視頻動(dòng)畫的性能問題和體積冗余。本文將詳細(xì)講解 Vue3 中 Lottie 的安裝、基礎(chǔ)使用、高級(jí)配置及實(shí)戰(zhàn)技巧。
一、核心優(yōu)勢(shì)
在開始集成前,先了解 Lottie 適配 Vue3 項(xiàng)目的核心價(jià)值:
- 輕量化:JSON 動(dòng)畫文件體積遠(yuǎn)小于 GIF / 視頻,且支持按需加載;
- 可交互:可通過代碼控制動(dòng)畫播放、暫停、跳轉(zhuǎn)、循環(huán)等,支持自定義交互邏輯;
- 矢量渲染:動(dòng)畫基于矢量,適配不同分辨率設(shè)備無模糊;
- Vue3 友好:支持組合式 API(Setup),可封裝為通用組件,復(fù)用性強(qiáng)。
二、環(huán)境準(zhǔn)備與安裝
1. 依賴安裝
Vue3 項(xiàng)目中推薦使用 lottie-web(官方 Web 端實(shí)現(xiàn)),通過 npm/yarn/pnpm 安裝:
# npm npm install lottie-web --save # yarn yarn add lottie-web # pnpm pnpm add lottie-web
2. 動(dòng)畫資源準(zhǔn)備
Lottie 依賴 AE 導(dǎo)出的 JSON 動(dòng)畫文件,獲取方式:
- 設(shè)計(jì)師使用 AE 制作動(dòng)畫,通過
Bodymovin插件導(dǎo)出 JSON 文件; - 從 Lottie 官方素材庫(kù)獲取免費(fèi)動(dòng)畫:LottieFiles。
將下載的 JSON 動(dòng)畫文件放入 Vue3 項(xiàng)目的 public 或 src/assets 目錄(推薦 public,避免打包路徑問題)。
三、基礎(chǔ)使用:封裝通用 Lottie 組件
為了在項(xiàng)目中復(fù)用,我們先封裝一個(gè)通用的 Lottie 組件(支持 Vue3 組合式 API)。
1. 創(chuàng)建 Lottie 通用組件
在 src/components 目錄下新建 LottieAnimation.vue:
<template>
<!-- 動(dòng)畫容器,需指定寬高 -->
<div ref="lottieContainer" class="lottie-container" :style="{ width, height }"></div>
</template>
<script setup>
import { ref, onMounted, onUnmounted, watch } from 'vue';
import lottie from 'lottie-web';
// 定義 Props
const props = defineProps({
// 動(dòng)畫 JSON 文件路徑
animationData: {
type: Object,
required: false,
default: null
},
path: {
type: String,
required: false,
default: ''
},
// 動(dòng)畫寬高
width: {
type: String,
default: '300px'
},
height: {
type: String,
default: '300px'
},
// 是否自動(dòng)播放
autoplay: {
type: Boolean,
default: true
},
// 是否循環(huán)播放
loop: {
type: Boolean,
default: true
},
// 動(dòng)畫速度(1 為正常速度)
speed: {
type: Number,
default: 1
},
// 渲染方式(svg/canvas/html),優(yōu)先 svg(矢量)
renderer: {
type: String,
default: 'svg',
validator: (val) => ['svg', 'canvas', 'html'].includes(val)
}
});
// 定義 Emits:暴露動(dòng)畫狀態(tài)事件
const emit = defineEmits(['complete', 'loopComplete', 'enterFrame']);
// 動(dòng)畫容器 Ref
const lottieContainer = ref(null);
// Lottie 實(shí)例
let lottieInstance = null;
// 初始化動(dòng)畫
const initLottie = () => {
if (!lottieContainer.value) return;
// 銷毀舊實(shí)例(避免重復(fù)渲染)
if (lottieInstance) {
lottieInstance.destroy();
}
// 創(chuàng)建 Lottie 實(shí)例
lottieInstance = lottie.loadAnimation({
container: lottieContainer.value, // 動(dòng)畫容器
animationData: props.animationData, // 動(dòng)畫 JSON 數(shù)據(jù)(本地導(dǎo)入)
path: props.path, // 動(dòng)畫 JSON 文件路徑(遠(yuǎn)程/ public 目錄)
renderer: props.renderer, // 渲染方式
loop: props.loop, // 循環(huán)播放
autoplay: props.autoplay, // 自動(dòng)播放
name: 'lottie-animation' // 動(dòng)畫名稱(可選)
});
// 設(shè)置動(dòng)畫速度
lottieInstance.setSpeed(props.speed);
// 監(jiān)聽動(dòng)畫事件
lottieInstance.addEventListener('complete', () => {
emit('complete'); // 動(dòng)畫播放完成
});
lottieInstance.addEventListener('loopComplete', () => {
emit('loopComplete'); // 動(dòng)畫循環(huán)完成
});
lottieInstance.addEventListener('enterFrame', (e) => {
emit('enterFrame', e); // 動(dòng)畫每一幀
});
};
// 監(jiān)聽 Props 變化,重新初始化
watch(
[() => props.path, () => props.animationData, () => props.loop, () => props.speed],
() => {
initLottie();
},
{ immediate: true }
);
// 組件卸載時(shí)銷毀實(shí)例
onUnmounted(() => {
if (lottieInstance) {
lottieInstance.destroy();
lottieInstance = null;
}
});
</script>
<style scoped>
.lottie-container {
display: inline-block;
overflow: hidden;
}
</style>
2. 基礎(chǔ)使用示例
在頁面組件中引入并使用封裝好的 LottieAnimation 組件,支持兩種加載方式:
方式 1:加載 public 目錄下的 JSON 文件
將動(dòng)畫文件 animation.json 放入 public/lottie/ 目錄,使用 path 傳入路徑:
<template>
<div class="demo-page">
<h2>Lottie 基礎(chǔ)使用示例</h2>
<LottieAnimation
path="/lottie/animation.json"
width="200px"
height="200px"
:loop="false"
:speed="1.2"
@complete="handleAnimationComplete"
/>
</div>
</template>
<script setup>
import LottieAnimation from '@/components/LottieAnimation.vue';
// 動(dòng)畫播放完成回調(diào)
const handleAnimationComplete = () => {
console.log('動(dòng)畫播放完成!');
};
</script>
方式 2:本地導(dǎo)入 JSON 文件(需配置 loader)
如果將動(dòng)畫文件放在 src/assets 目錄,需先導(dǎo)入 JSON 文件(Vue3 + Vite 無需額外配置,Webpack 需確保支持 JSON 導(dǎo)入):
<template>
<div class="demo-page">
<LottieAnimation
:animation-data="animationData"
width="200px"
height="200px"
:autoplay="false"
ref="lottieRef"
/>
<button @click="playAnimation">播放動(dòng)畫</button>
<button @click="pauseAnimation">暫停動(dòng)畫</button>
</div>
</template>
<script setup>
import { ref } from 'vue';
import LottieAnimation from '@/components/LottieAnimation.vue';
// 導(dǎo)入本地 JSON 動(dòng)畫文件
import animationData from '@/assets/lottie/animation.json';
const animationData = ref(animationData);
const lottieRef = ref(null);
// 播放動(dòng)畫
const playAnimation = () => {
lottieRef.value.lottieInstance.play();
};
// 暫停動(dòng)畫
const pauseAnimation = () => {
lottieRef.value.lottieInstance.pause();
};
</script>
四、高級(jí)配置與交互控制
Lottie 提供了豐富的 API 用于控制動(dòng)畫,以下是常用的交互場(chǎng)景:
1. 手動(dòng)控制播放 / 暫停 / 停止
通過獲取 Lottie 實(shí)例,調(diào)用內(nèi)置方法:
// 播放動(dòng)畫 lottieInstance.play(); // 暫停動(dòng)畫 lottieInstance.pause(); // 停止動(dòng)畫(重置到第一幀) lottieInstance.stop(); // 跳轉(zhuǎn)到指定幀(frameNum 為幀編號(hào)) lottieInstance.goToAndStop(frameNum, true); // 跳轉(zhuǎn)到指定幀并播放 lottieInstance.goToAndPlay(frameNum, true);
2. 動(dòng)態(tài)修改動(dòng)畫速度
javascript
運(yùn)行
// 設(shè)置速度(0.5 為慢放,2 為快放) lottieInstance.setSpeed(0.5); // 獲取當(dāng)前速度 const currentSpeed = lottieInstance.playSpeed;
3. 控制循環(huán)模式
// 設(shè)置循環(huán)次數(shù)(0 為無限循環(huán),1 為播放 1 次) lottieInstance.loop = 0; // 單獨(dú)設(shè)置循環(huán)(立即生效) lottieInstance.setLoop(true); // 無限循環(huán) lottieInstance.setLoop(3); // 循環(huán) 3 次
4. 監(jiān)聽動(dòng)畫進(jìn)度
通過 enterFrame 事件監(jiān)聽動(dòng)畫進(jìn)度,實(shí)現(xiàn)進(jìn)度條聯(lián)動(dòng):
<template>
<LottieAnimation
path="/lottie/animation.json"
@enterFrame="handleEnterFrame"
/>
<input
type="range"
min="0"
max="100"
v-model="progress"
@input="handleProgressChange"
/>
</template>
<script setup>
import { ref } from 'vue';
const progress = ref(0);
let totalFrames = 0;
// 監(jiān)聽每一幀,更新進(jìn)度
const handleEnterFrame = (e) => {
totalFrames = e.totalFrames;
progress.value = Math.floor((e.currentFrame / totalFrames) * 100);
};
// 拖動(dòng)進(jìn)度條,跳轉(zhuǎn)動(dòng)畫
const handleProgressChange = () => {
const targetFrame = (progress.value / 100) * totalFrames;
lottieInstance.goToAndPlay(targetFrame, true);
};
</script>
五、性能優(yōu)化與注意事項(xiàng)
1. 性能優(yōu)化
- 懶加載:非首屏動(dòng)畫使用
v-if或動(dòng)態(tài)導(dǎo)入,按需初始化; - 銷毀實(shí)例:組件卸載時(shí)務(wù)必調(diào)用
destroy()銷毀實(shí)例,避免內(nèi)存泄漏; - 選擇渲染方式:優(yōu)先使用
svg渲染(矢量、輕量),復(fù)雜動(dòng)畫可使用canvas; - 壓縮 JSON 文件:使用 LottieFiles 在線工具壓縮動(dòng)畫 JSON,減少體積。
2. 常見問題解決
- 動(dòng)畫不顯示:檢查容器寬高是否設(shè)置、JSON 文件路徑是否正確(
path以/開頭表示 public 根目錄); - 動(dòng)畫卡頓:減少動(dòng)畫層數(shù)和復(fù)雜路徑,避免同時(shí)播放多個(gè)大型動(dòng)畫;
- 跨域問題:遠(yuǎn)程加載 JSON 文件需確保服務(wù)端開啟 CORS;
- Vue3 打包路徑問題:
animationData導(dǎo)入本地 JSON 時(shí),Vite 需確保assetsInclude包含.json(默認(rèn)已支持)。
六、總結(jié)
Lottie 是 Vue3 項(xiàng)目中實(shí)現(xiàn)高品質(zhì)動(dòng)畫的最佳選擇之一,通過封裝通用組件可快速集成到項(xiàng)目中,結(jié)合其豐富的 API 能實(shí)現(xiàn)靈活的交互控制。本文從基礎(chǔ)安裝、組件封裝、高級(jí)交互到性能優(yōu)化,覆蓋了 Lottie 在 Vue3 中的核心使用場(chǎng)景。合理使用 Lottie 可顯著提升頁面交互體驗(yàn),同時(shí)兼顧性能與兼容性。
如果需要更復(fù)雜的場(chǎng)景(如動(dòng)畫分段播放、結(jié)合 Vuex/Pinia 控制動(dòng)畫狀態(tài)),可基于本文的通用組件擴(kuò)展,結(jié)合業(yè)務(wù)需求定制化開發(fā)。
到此這篇關(guān)于Vue3 中 Lottie動(dòng)畫庫(kù)的使用指南的文章就介紹到這了,更多相關(guān)Vue3 Lottie動(dòng)畫庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)折線圖 可按時(shí)間查詢
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)折線圖,可按時(shí)間查詢,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
Vue指令修飾符,v-bind對(duì)樣式控制的增強(qiáng),computed計(jì)算屬性,watch監(jiān)視器詳解
這篇文章主要介紹了Vue指令修飾符,v-bind對(duì)樣式控制的增強(qiáng),computed計(jì)算屬性,watch監(jiān)視器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06
使用vue.js開發(fā)時(shí)一些注意事項(xiàng)
使用vue.js進(jìn)行項(xiàng)目的開發(fā)已經(jīng)有了一定的時(shí)間,在任務(wù)的過程中以及和不同的開發(fā)使用者交流中,逐漸對(duì)vue.js的使用心得有了一定的積累。本文主要給大家分享一些開發(fā)時(shí)需要注意的事項(xiàng)2016-04-04
vue中使用window.open()參數(shù)示例詳解
這篇文章主要介紹了vue中使用window.open()參數(shù)詳解,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
vite+element-plus項(xiàng)目基礎(chǔ)搭建的全過程
最近看完Vue3和Vite文檔之后,就寫了個(gè)小demo,整體感覺下來還是很絲滑的,下面這篇文章主要給大家介紹了關(guān)于vite+element-plus項(xiàng)目基礎(chǔ)搭建的全過程,需要的朋友可以參考下2022-07-07
前端vue-cropperjs實(shí)現(xiàn)圖片裁剪方案
這篇文章主要為大家介紹了前端vue-cropperjs實(shí)現(xiàn)圖片裁剪方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

