最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在Vue前端(Vue2/Vue3?通用)載入JSON格式的動圖實例代碼

 更新時間:2025年11月04日 09:10:45   作者:睡美人的小仙女127  
這篇文章主要介紹了在Vue前端(Vue2/Vue3?通用)載入JSON格式動圖的相關(guān)資料,文中通過代碼講解了環(huán)境配置、組件集成、動畫控制等,重點介紹了Lottie的loadAnimation方法的參數(shù)配置和常見問題解決方法,需要的朋友可以參考下

前言

在 Vue 前端(Vue2/Vue3 通用)載入 JSON 格式的動圖,核心是使用 Lottie 動畫庫(由 Airbnb 開發(fā),專門解析 JSON 動畫文件)。以下是分步驟的完整實現(xiàn)方案,包含環(huán)境配置、組件集成、動畫控制等核心功能:

一、核心原理

JSON 格式的動圖(如 Lottie 動畫)本質(zhì)是通過 JSON 文件描述動畫的關(guān)鍵幀、路徑、屬性變化等信息,再由 Lottie 庫解析并渲染為 SVG/Canvas 動畫。相比 GIF/APNG,它支持矢量無損縮放、體積更小、可交互控制(暫停 / 播放 / 反向)。

二、前置準備

  1. 獲取 JSON 動畫文件

    • 設(shè)計工具導(dǎo)出:用 Adobe After Effects 制作動畫,通過「Bodymovin」插件導(dǎo)出為 JSON 格式(Bodymovin 插件下載);
    • 在線資源:從 LottieFiles 下載現(xiàn)成的 JSON 動畫(免費 / 付費資源);
    • 本地存放:將 JSON 文件放在項目 src/assets/animations/ 目錄下(如 menu.jsonloading.json)。
  2. 安裝 Lottie 依賴
    Lottie 官方提供 lottie-web 庫,支持 Vue/React 等框架,執(zhí)行以下命令安裝:

    # npm 安裝
    npm install lottie-web --save
    
    # yarn 安裝
    yarn add lottie-web
    

三、Vue 組件集成(Vue2/Vue3 通用)

以下以「菜單按鈕 JSON 動圖」為例,實現(xiàn)完整的動畫載入與控制邏輯。

1. 基礎(chǔ)版:自動播放的 JSON 動圖

適用于無需交互的場景(如裝飾性動畫、加載動畫)。

<template>
  <div class="animation-container">
    <!-- 動畫容器:用于承載 Lottie 渲染的動畫 -->
    <div ref="lottieRef" class="lottie-box"></div>
  </div>
</template>

<script>
// 1. 引入 Lottie 庫
import lottie from 'lottie-web';

export default {
  name: 'LottieAnimation',
  data() {
    return {
      animationInstance: null, // 存儲動畫實例(用于后續(xù)控制)
    };
  },
  mounted() {
    // 2. 組件掛載后初始化動畫
    this.initLottie();
  },
  beforeUnmount() {
    // 3. 組件銷毀前釋放動畫資源(避免內(nèi)存泄漏)
    if (this.animationInstance) {
      this.animationInstance.destroy();
    }
  },
  methods: {
    initLottie() {
      // 加載 JSON 動畫并渲染
      this.animationInstance = lottie.loadAnimation({
        container: this.$refs.lottieRef, // 動畫容器(通過 ref 獲取 DOM)
        renderer: 'svg', // 渲染方式:svg(推薦,矢量無損)/ canvas / html
        loop: true, // 是否循環(huán)播放(true/false/數(shù)字,如 3 表示循環(huán)3次)
        autoplay: true, // 是否自動播放
        // 兩種加載 JSON 的方式(二選一):
        path: require('@/assets/animations/menu.json'), // 方式1:通過路徑加載(推薦,支持按需加載)
        // animationData: require('@/assets/animations/menu.json'), // 方式2:直接導(dǎo)入 JSON 數(shù)據(jù)(適合小文件)
      });

      // 可選:監(jiān)聽動畫事件(如播放完成、幀變化)
      this.animationInstance.addEventListener('complete', () => {
        console.log('動畫播放完成(僅非循環(huán)時觸發(fā))');
      });
    },
  },
};
</script>

<style scoped>
.animation-container {
  /* 父容器樣式,根據(jù)布局調(diào)整 */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.lottie-box {
  /* 動畫尺寸:與設(shè)計稿一致,支持百分比/固定像素 */
  width: 60px; 
  height: 60px;
  cursor: pointer; /* 如需點擊交互,添加指針樣式 */
}
</style>

2. 進階版:可交互控制的 JSON 動圖

適用于需要手動控制的場景(如點擊切換動畫狀態(tài)、暫停 / 播放)。

<template>
  <div class="interactive-animation">
    <!-- 動畫容器 -->
    <div ref="lottieRef" class="lottie-box" @click="toggleAnimation"></div>
    <!-- 控制按鈕 -->
    <div class="control-buttons">
      <button @click="playAnimation">播放</button>
      <button @click="pauseAnimation">暫停</button>
      <button @click="reverseAnimation">反向播放</button>
    </div>
  </div>
</template>

<script>
import lottie from 'lottie-web';

export default {
  name: 'InteractiveLottie',
  data() {
    return {
      animationInstance: null,
      isPlaying: true, // 標記當前是否在播放
    };
  },
  mounted() {
    this.initLottie();
  },
  beforeUnmount() {
    if (this.animationInstance) {
      this.animationInstance.destroy();
    }
  },
  methods: {
    initLottie() {
      this.animationInstance = lottie.loadAnimation({
        container: this.$refs.lottieRef,
        renderer: 'svg',
        loop: false, // 關(guān)閉自動循環(huán)(手動控制)
        autoplay: true,
        path: require('@/assets/animations/menu.json'),
      });

      // 監(jiān)聽播放狀態(tài)變化
      this.animationInstance.addEventListener('play', () => {
        this.isPlaying = true;
      });
      this.animationInstance.addEventListener('pause', () => {
        this.isPlaying = false;
      });
    },

    // 1. 點擊動畫容器切換播放/暫停
    toggleAnimation() {
      if (this.isPlaying) {
        this.animationInstance.pause();
      } else {
        this.animationInstance.play();
      }
    },

    // 2. 播放動畫
    playAnimation() {
      this.animationInstance.play();
    },

    // 3. 暫停動畫
    pauseAnimation() {
      this.animationInstance.pause();
    },

    // 4. 反向播放動畫
    reverseAnimation() {
      this.animationInstance.setDirection(-1); // 設(shè)置方向:-1 反向,1 正向
      this.animationInstance.play();
    },
  },
};
</script>

<style scoped>
.interactive-animation {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 15px;
  padding: 20px;
}

.lottie-box {
  width: 80px;
  height: 80px;
}

.control-buttons {
  display: flex;
  gap: 10px;
}

button {
  padding: 6px 12px;
  border: 1px solid #1890ff;
  background: #fff;
  color: #1890ff;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

button:hover {
  background: #1890ff;
  color: #fff;
}
</style>

四、關(guān)鍵配置說明

Lottie 的 loadAnimation 方法支持以下核心參數(shù),根據(jù)需求調(diào)整:

參數(shù)名類型說明
containerDOM 元素必選,動畫渲染的容器(通過 Vue 的 ref 獲?。?/td>
renderer字符串可選,渲染方式:svg(推薦,矢量無損)、canvas(性能好)、html
loop布爾 / 數(shù)字可選,是否循環(huán):true(無限循環(huán))、false(僅播放一次)、3(循環(huán) 3 次)
autoplay布爾可選,是否自動播放:true(默認)、false
path字符串可選,JSON 文件路徑(如 require('@/assets/animations/xxx.json')
animationDataJSON 對象可選,直接傳入 JSON 數(shù)據(jù)(適合小文件,避免額外請求)
name字符串可選,動畫名稱(用于多動畫管理)

五、常見問題解決

  1. JSON 動畫加載失敗

    • 檢查路徑:確保 path 中的 JSON 文件路徑正確(如 @/assets/animations/menu.json,@ 對應(yīng) src 目錄);
    • 檢查文件格式:確保 JSON 文件語法正確(可通過 JSON 校驗工具 驗證);
    • 跨域問題:若 JSON 文件放在 CDN 上,需確保服務(wù)器開啟 CORS 跨域支持。
  2. 動畫尺寸異常

    • 給 lottie-box 容器設(shè)置固定寬高(如 width: 100px; height: 100px),避免自適應(yīng)導(dǎo)致拉伸;
    • 通過 Lottie 實例調(diào)整尺寸:
      this.animationInstance.setSize(120, 120); // 動態(tài)設(shè)置寬高(單位px)
      
  3. Vue3 中 this.$refs 獲取不到 DOM
    Vue3 組合式 API 中需用 ref 函數(shù)獲取 DOM,示例:

    <script setup>
    import { ref, onMounted, onBeforeUnmount } from 'vue';
    import lottie from 'lottie-web';
    
    const lottieRef = ref(null); // 替代 Vue2 的 this.$refs.lottieRef
    let animationInstance = null;
    
    onMounted(() => {
      animationInstance = lottie.loadAnimation({
        container: lottieRef.value, // 注意:Vue3 需用 .value 獲取 DOM
        path: require('@/assets/animations/menu.json'),
        loop: true,
        autoplay: true,
      });
    });
    
    onBeforeUnmount(() => {
      if (animationInstance) animationInstance.destroy();
    });
    </script>
    

六、總結(jié)

  1. 核心流程:安裝 lottie-web → 準備 JSON 動畫文件 → 組件中通過 lottie.loadAnimation 初始化 → (可選)添加交互控制;
  2. 優(yōu)勢:矢量無損縮放、體積小、可交互、跨平臺(Web / 移動端通用);
  3. 適用場景:按鈕動效、加載動畫、廣告 Banner、頁面過渡動畫等。

按照以上步驟,即可在 Vue 前端輕松載入并控制 JSON 格式的動圖,替代傳統(tǒng)的靜態(tài)圖片或低質(zhì)量動圖。

到此這篇關(guān)于在Vue前端(Vue2/Vue3 通用)載入JSON格式動圖的文章就介紹到這了,更多相關(guān)Vue前端載入JSON格式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue實現(xiàn)圖片預(yù)覽組件封裝與使用

    vue實現(xiàn)圖片預(yù)覽組件封裝與使用

    這篇文章主要為大家詳細介紹了vue實現(xiàn)圖片預(yù)覽組件封裝與使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • vue3.0報錯Cannot?find?module‘worker_threads‘的解決辦法

    vue3.0報錯Cannot?find?module‘worker_threads‘的解決辦法

    這篇文章介紹了vue3.0報錯Cannot?find?module‘worker_threads‘的解決辦法。對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2021-11-11
  • 詳解Vue 匿名、具名和作用域插槽的使用方法

    詳解Vue 匿名、具名和作用域插槽的使用方法

    這篇文章主要介紹了Vue 匿名、具名和作用域插槽的使用方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04
  • 淺談在vue項目中如何定義全局變量和全局函數(shù)

    淺談在vue項目中如何定義全局變量和全局函數(shù)

    本篇文章主要介紹了淺談在vue項目中如何定義全局變量和全局函數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 實現(xiàn)shallowReadonly和isProxy功能示例詳解

    實現(xiàn)shallowReadonly和isProxy功能示例詳解

    這篇文章主要為大家介紹了實現(xiàn)shallowReadonly和isProxy功能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Vue中methods實現(xiàn)原理是什么

    Vue中methods實現(xiàn)原理是什么

    methods是如何綁定this的 methods綁定上下文執(zhí)行環(huán)境是通過bind來進行的呢,本文給大家介紹Vue中methods實現(xiàn)原理是什么,感興趣的朋友一起看看吧
    2023-11-11
  • 使用sessionStorage解決vuex在頁面刷新后數(shù)據(jù)被清除的問題

    使用sessionStorage解決vuex在頁面刷新后數(shù)據(jù)被清除的問題

    localStorage沒有時間期限,除非將它移除,sessionStorage即會話,當瀏覽器關(guān)閉時會話結(jié)束,有時間期限,具有自行百度。本文使用的是sessionStorage解決vuex在頁面刷新后數(shù)據(jù)被清除的問題,需要的朋友可以參考下
    2018-04-04
  • Vue?(Vuex)中?store?基本用法

    Vue?(Vuex)中?store?基本用法

    Vuex?是一個專為?Vue.js?應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測的方式發(fā)生變化,這篇文章主要介紹了Vue?中?store?基本用法,需要的朋友可以參考下
    2023-01-01
  • 詳解如何在nuxt中添加proxyTable代理

    詳解如何在nuxt中添加proxyTable代理

    這篇文章主要介紹了詳解如何在nuxt中添加proxyTable代理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • vue的一個分頁組件的示例代碼

    vue的一個分頁組件的示例代碼

    本篇文章主要介紹了vue的一個分頁組件的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12

最新評論

临夏市| 江陵县| 永寿县| 弥渡县| 马公市| 青海省| 黄大仙区| 澄城县| 玉林市| 嘉义市| 大渡口区| 青田县| 承德市| 株洲市| 色达县| 竹北市| 壶关县| 黑龙江省| 马关县| 岳池县| 荥阳市| 沂源县| 云阳县| 周至县| 临清市| 长沙市| 武邑县| 沾益县| 石狮市| 新巴尔虎左旗| 安丘市| 新竹县| 云浮市| 娱乐| 蓬莱市| 隆德县| 颍上县| 宜川县| 新干县| 璧山县| 天等县|