使用lottie操作動(dòng)畫(huà),lottie for web,入門(mén)點(diǎn)贊動(dòng)畫(huà)

lottie可以讓前端更好的控制動(dòng)畫(huà),既能使動(dòng)畫(huà)流暢運(yùn)行又可以進(jìn)行一定的干預(yù)。
lottie是用設(shè)計(jì)師使用AE制作動(dòng)畫(huà)搁嗓,然后用bodymovin插件去導(dǎo)出的json,前端工程師使用這個(gè)json去實(shí)現(xiàn)動(dòng)畫(huà)箱靴。
這個(gè)動(dòng)畫(huà)更加依賴設(shè)計(jì)師腺逛。前端工程師使用對(duì)應(yīng)的api可以對(duì)動(dòng)畫(huà)進(jìn)行一些事件上的操作,主要是操作時(shí)間流衡怀。

lottie

首先安裝lottie

npm install lottie-web

在組件內(nèi)引用

import lottie from 'lottie-web';

初始化lottie


<div id="bm" @click="lottieClick"></div>
//在mounted里引用棍矛,注意鉤子的生命周期,created里#bm這個(gè)元素還未創(chuàng)建
this.lottie = lottie.loadAnimation({
      container: document.getElementById('bm'),
      renderer: 'svg',
      loop: false,
      autoplay: false,
      animationData: data
});
這是我制作的一個(gè)簡(jiǎn)單應(yīng)用抛杨。
效果圖

github地址

下面貼一下這個(gè)效果的組件源代碼


<template>
    <div class="box">
        <div id="bm" @click="lottieClick"></div>

        <button v-for="(value,key) in dataJson" :key="key" @click="changeFun(value)"> {{key}}</button>
        <button @click="speedFun(speed++)  ">speed++</button>
        <button @click="speedFun(speed--)  ">speed--</button>
        
    </div>
</template>

<script>
    import lottie from 'lottie-web';
    
    export default {
        name: 'lottie',
        components: {},
        data() {
            return {
                speed: 1,
                lottie: '',
                isAnimationLoad: true,
                dataJson: {
                    heart1: require('../assets/data'),
                    heart2: require('../assets/lottie (1)'),
                    hand1: require('../assets/lottie (2)'),
                    hand2: require('../assets/lottie')
                }
            };
        },
        computed: {},
        props: {},
        created() {
        },
        methods: {
            speedFun(value) {
                if (value < 1) {
                    value = 1;
                }

                this.lottie.setSpeed(value);
            },
            changeFun(data) {
                console.log();
                if (this.lottie != '') {
                    this.lottie.destroy();
                }
                this.isAnimationLoad = true;
                this.initLottie(data);
            },
            lottieClick() {
                if (this.isAnimationLoad) {
                    this.isAnimationLoad = false;
                    if (this.lottie.currentFrame == this.lottie.totalFrames - 1) {
                        this.lottie.setDirection(-1);
                        this.lottie.play();
                    } else {
                        this.lottie.setDirection(1);
                        this.lottie.play();
                    }
                }
            },
            completeFun() {
                this.isAnimationLoad = true;
            },
            initLottie(data) {
                this.lottie = lottie.loadAnimation({
                    container: document.getElementById('bm'),
                    renderer: 'svg',
                    loop: false,
                    autoplay: false,
                    animationData: data
                });

                //初始化狀態(tài)
                // this.lottie.goToAndStop(this.lottie.totalFrames - 1, true);
                this.lottie.addEventListener('complete', this.completeFun);
            }
        },
        mounted() {
            this.initLottie(this.dataJson.heart1);
        },
        watch: {},
        destroyed() {
            //頁(yè)面銷毀時(shí)
        }
    };
</script>
<style lang="scss" scoped>

    .box {
        padding-top: 50px;
        padding-left: 50px;

        div {
            border: 1px solid #efefef;
            width: 100px;
            height: 100px;
        }

        button {
            padding: 4px 10px;
            margin-top: 20px;
            margin-right: 20px;
        }
    }
</style>

動(dòng)畫(huà)實(shí)例有以下主要方法:

animation instances have these main methods:

  • anim.play()
  • anim.stop()
  • anim.pause()
  • anim.setLocationHref(locationHref) -- one param usually pass as location.href. Its useful when you experience mask issue in safari where your url does not have # symbol.
  • anim.setSpeed(speed) -- one param speed (1 is normal speed)
  • anim.goToAndStop(value, isFrame) first param is a numeric value. second param is a boolean that defines time or frames for first param
  • anim.goToAndPlay(value, isFrame) first param is a numeric value. second param is a boolean that defines time or frames for first param
  • anim.setDirection(direction) -- one param direction (1 is normal direction.)
  • anim.playSegments(segments, forceFlag) -- first param is a single array or multiple arrays of two values each(fromFrame,toFrame), second param is a boolean for forcing the new segment right awa
  • anim.setSubframe(flag) -- If false, it will respect the original AE fps. If true, it will update as much as possible. (true by default
  • anim.destroy()

lottie has 8 main methods:

  • lottie.play() -- with 1 optional parameter name to target a specific animation
  • lottie.stop() -- with 1 optional parameter name to target a specific animation
  • lottie.setSpeed() -- first param speed (1 is normal speed) -- with 1 optional parameter name to target a specific animation
  • lottie.setDirection() -- first param direction (1 is normal direction.) -- with 1 optional parameter name to target a specific animation
  • lottie.searchAnimations() -- looks for elements with class "lottie"
  • lottie.loadAnimation() -- Explained above. returns an animation instance to control individually.
  • lottie.destroy() -- To destroy and release resources. The DOM element will be emptied.
  • lottie.registerAnimation() -- you can register an element directly with registerAnimation. It must have the "data-animation-path" attribute pointing at the data.json url
  • lottie.setQuality() -- default 'high', set 'high','medium','low', or a number > 1 to improve player performance. In some animations as low as 2 won't show any difference.

請(qǐng)?jiān)敿?xì)查看lottie官方文檔
本次使用的json是由螞蟻設(shè)計(jì)生成

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末够委,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子怖现,更是在濱河造成了極大的恐慌茁帽,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件屈嗤,死亡現(xiàn)場(chǎng)離奇詭異潘拨,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)饶号,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門(mén)铁追,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人茫船,你說(shuō)我怎么就攤上這事脂信⊙Ⅲ” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵狰闪,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我濒生,道長(zhǎng)埋泵,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任罪治,我火速辦了婚禮丽声,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘觉义。我一直安慰自己雁社,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布晒骇。 她就那樣靜靜地躺著霉撵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪洪囤。 梳的紋絲不亂的頭發(fā)上徒坡,一...
    開(kāi)封第一講書(shū)人閱讀 52,156評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音瘤缩,去河邊找鬼喇完。 笑死,一個(gè)胖子當(dāng)著我的面吹牛剥啤,可吹牛的內(nèi)容都是我干的锦溪。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼府怯,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼刻诊!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起富腊,我...
    開(kāi)封第一講書(shū)人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤坏逢,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后赘被,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體是整,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年民假,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了浮入。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡羊异,死狀恐怖事秀,靈堂內(nèi)的尸體忽然破棺而出彤断,到底是詐尸還是另有隱情,我是刑警寧澤易迹,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布宰衙,位于F島的核電站,受9級(jí)特大地震影響睹欲,放射性物質(zhì)發(fā)生泄漏供炼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一窘疮、第九天 我趴在偏房一處隱蔽的房頂上張望袋哼。 院中可真熱鬧,春花似錦闸衫、人聲如沸涛贯。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)弟翘。三九已至,卻和暖如春身冬,著一層夾襖步出監(jiān)牢的瞬間衅胀,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工酥筝, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留滚躯,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓嘿歌,卻偏偏與公主長(zhǎng)得像掸掏,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子宙帝,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359