vue-lottie動畫效果

vue-lottie動畫效果

以前用lottie模仿過san官網的動畫效果(沒有打廣告QAQ)html

倉庫地址vue

模仿demogit

bloggithub

掘金web

vue項目 用到了vue-lottie動畫效果spring

用lottie的好處有不少(.......此處省略n字) 簡單來講就是簡單高效的還原設計的動畫效果npm

而後在我的項目使用vue-lottie 分享一些小小經驗吧json

廢話很少說~~~ (正經分割線)bash


分析官方demo

先來一個簡單的嚐嚐鮮app

vue-lottie倉庫

vue-lottie demo

打開倉庫能夠看見不少很棒的效果(nice

Installation

npm install --save vue-lottie
複製代碼

Usage

<template>
    <div id="app">
        <lottie :options="defaultOptions" :height="400" :width="400" v-on:animCreated="handleAnimation"/>
        <div>
            <p>Speed: x{{animationSpeed}}</p>
            <input type="range" value="1" min="0" max="3" step="0.5"
                   v-on:change="onSpeedChange" v-model="animationSpeed">
        </div>
        <button v-on:click="stop">stop</button>
        <button v-on:click="pause">pause</button>
        <button v-on:click="play">play</button>
    </div>
</template>

<script>
  import Lottie from './lottie.vue';
  import * as animationData from './assets/pinjump.json';

  export default {
    name: 'app',
    components: {
      'lottie': Lottie
    },
    data() {
      return {
        defaultOptions: {animationData: animationData},
        animationSpeed: 1
      }
    },
    methods: {
      handleAnimation: function (anim) {
        this.anim = anim;
      },

      stop: function () {
        this.anim.stop();
      },

      play: function () {
        this.anim.play();
      },

      pause: function () {
        this.anim.pause();
      },

      onSpeedChange: function () {
        this.anim.setSpeed(this.animationSpeed);
      }
    }
  }
</script>
複製代碼

這是以前官方給的demo代碼 基本上和平時使用沒啥不同(因此只須要複製粘貼就ok了)

# json 動畫效果AE轉json後的文件
import * as animationData from './assets/pinjump.json';
複製代碼

引入的json須要改!!!

# 這裏引入了&emsp;lottie組件
import Lottie from './lottie.vue';
複製代碼
# lottie.vue
<template>
    <div :style="style" ref="lavContainer"></div>
</template>

<script>
  import lottie from 'lottie-web'
export default {
    props: {
      options: {
        type: Object,
        required: true
      },
      height: Number,
      width: Number
    },
    data() {
      return {
        style: {
          width: this.width ? `${this.width}px` : '100%',
          height: this.height ? `${this.height}px` : '100%',
          overflow: 'hidden',
          margin: '0 auto'
        }
      }
    },
    mounted() {
      this.anim = lottie.loadAnimation({
        container: this.$refs.lavContainer,
        renderer: 'svg',
        loop: this.options.loop !== false,
        autoplay: this.options.autoplay !== false,
        animationData: this.options.animationData,
        rendererSettings: this.options.rendererSettings
      }
      )
      this.$emit('animCreated', this.anim)
    }
  }
</script>
複製代碼

而後會發現仍是有錯誤(缺乏組件!) 其實很簡單啦,打開倉庫進入src而後打開lottle組件而後複製過去就ok啦hhh(簡單)

效果圖

這是效果圖(是否是很簡單233

使用別的json文件

官方給給了一個很好的效果網站 地址

下載json文件 而後更換引入的json

# json 動畫效果AE轉json後的文件
import * as animationData from './assets/blood_transfusion_kawaii.json.json';

複製代碼

效果圖

是否是也很簡單!!!

使用vue-lottie模仿san官網的動畫效果

先來效果圖~~~

效果圖

由於有多個須要用到lottie動畫,想了半天不知道怎麼解決調用方法的問題 最後想了一個簡單的方法

直接將每個動畫抽到一個組件 組件內依然用以前的方法(稍微改造一下就行

而後利用父子組件傳數據的形式傳遞json文件 子組件props接收

# html
<template>
  <div class="card-panel" @mouseenter="lottiePlay" @mouseleave="lottieStop">
    <div class="card-panel-icon-wrapper icon-shoppingCard">
      <lottie :options="defaultOptions" :height="80" :width="80" v-on:animCreated="handleAnimation" />
    </div>
    <div class="card-panel-description">
      <div class="card-panel-text">今日活躍</div>
      <div class="card-panel-num">2600</div>
    </div>
  </div>
</template>
複製代碼
# props
props: {
    animationDataPath: {
      type: Object,
      default: null
    }
  },
  data() {
  return {
    defaultOptions: {
      // animationData: animationDataPath,
      animationData: this.animationDataPath,
      autoplay: false,  # 不自動播放
      loop: false&emsp;&emsp;&emsp;&emsp;&emsp;# 不循環
    }
  }
}
複製代碼
# 事件調用
@mouseenter="lottiePlay" @mouseleave="lottieStop"

lottiePlay: function() {
  this.anim.play()
},
lottieStop: function() {
  this.anim.stop()
}
複製代碼

而後就到了父組件傳數據

# 父組件
<panel-lottie :animationDataPath="animationDataPathOne"></panel-lottie>

animationDataPathOne: require('../../../public/json/compass.json')
複製代碼

本身用到了require引入json 而後打包出來 同樣能夠正常運行 若是你們有很好的方法能夠教我!我好學習學習


emmmmm 大概就是這麼多吧~

若是實在須要這個的源碼能夠打開個人github倉庫 因爲項目仍是一個半成品 因此地址就放在最後面了

vue-lottie源碼

項目地址

若是你們以爲不錯的話 能夠點star哦(厚臉皮233

QQ 952822399

新開了個Qq羣,你們也能夠進來互相交流~ iD 718639024

相關文章
相關標籤/搜索