Javascript動(dòng)畫插件lottie-web的使用方法
lottie可以將一個(gè)json數(shù)據(jù)渲染成一個(gè)動(dòng)畫,而且支持多平臺(tái),在不同的平臺(tái)下使用同一個(gè)json文件即可實(shí)現(xiàn)相同的效果,非常的方便。這里介紹前端的使用方法。
https://github.com/airbnb/lottie-web
1.配合vue-cli使用
1.npm安裝lottie-web
npm install lottie-web
2.創(chuàng)建loading.vue
2.1引入lottie插件和需要的json數(shù)據(jù)
2.2接收父組件傳入的配置參數(shù)
2.3在頁(yè)面渲染完畢后進(jìn)行初始化
<template>
<!-- 為容器綁定樣式 -->
<div :style="style" ref="lavContainer"></div>
</template>
<script>
//引入lottie
import lottie from 'lottie-web'
//引入json數(shù)據(jù)
import animationData from '../../static/bitcoin.json'
export default {
props: { //接收父元素傳入的參數(shù)
options: {
type: Object,
required: true
},
height: Number,
width: Number
},
data() {
return {
style: { //設(shè)置容器的樣式
width: this.width ? `${this.width}px` : '40%', //如果父元素有傳參則使用傳參的值,沒有則=40%
height: this.height ? `${this.height}px` : '100%',//如果父元素有傳參則使用傳參的值,沒有則=100%
overflow: 'hidden',//超出容器隱藏
margin: '0 auto' //水平居中
}
}
},
mounted() {
lottie.loadAnimation({ //初始化
container: this.$refs.lavContainer,//在哪個(gè)dom容器中生效
renderer: 'svg',//渲染方式svg
loop: this.options.loop !== false,//是否循環(huán) 如果傳入的參數(shù)options.loop不為false 則一直循環(huán) 即默認(rèn)循環(huán)
autoplay: this.options.autoplay !== false,//是否自動(dòng)播放 如果傳入的參數(shù)options.autoplay不為false 則自動(dòng)播放 即默認(rèn)自動(dòng)播放
animationData: animationData,//動(dòng)畫數(shù)據(jù),這個(gè)必須要有
rendererSettings: this.options.rendererSettings
})
}
}
</script>3.父組件引用loadding.vue
可以為loadding組件設(shè)定一個(gè)容器,通過空中這個(gè)容器的display屬性來控制loadding組件的顯示和隱藏
<template>
<div class="test_wrap">
<div v-show="show">
<loadding :options="defaultOptions" />
</show>
</div>
</template>
<script>
//引入子組件
import loadding from '@/components/loadding'
export default {
data () {
return {
show:false,
defaultOptions: {
autoplay: true,//自動(dòng)播放
loop: true,//循環(huán)播放
},
}
},
components: {
'loadding': loadding
}
}
</script>2.在HTML頁(yè)面中使用
1.引入lottie-web這個(gè)插件 可以使用cdn,也可以引入本地的
<script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.6.8/lottie_svg.min.js"> </script>
2.頁(yè)面加載完畢后,初始化,并傳入相應(yīng)的配置項(xiàng)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.6.8/lottie_svg.min.js"></script>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<title>Document</title>
</head>
<body>
<div class="box"></div>
</body>
</html>
<script>
$(function(){
//getJSON() 方法使用 AJAX 的 HTTP GET 請(qǐng)求獲取 JSON 數(shù)據(jù)
$.getJSON("./betcoin.json",function(result){
//獲取到數(shù)據(jù)后進(jìn)行初始化
console.log(result)
lottie.loadAnimation({ //初始化
container: document.querySelector('.box'),//在哪個(gè)dom容器中生效
renderer: 'svg',//渲染方式svg
loop: true,//循環(huán)
autoplay: true,//自動(dòng)播放
animationData: result,//動(dòng)畫數(shù)據(jù)
})
})
})
</script>
注意:json數(shù)據(jù)使用了ajax進(jìn)行獲取,要留意跨域問題。
為了避免跨域或者本地讀取時(shí)的權(quán)限問題,可以講json文件的數(shù)據(jù)用 反引號(hào) `` 包裹起來,用一個(gè)全局變量保存,然后保存為betcoin2.js,即可使用這個(gè)數(shù)據(jù)了,記得用JSON.parse()將json字符串轉(zhuǎn)換回對(duì)象格式
//betcoin2.js var animationData = `省略,里面為原json對(duì)象`
<script src="./betcoin2.js"></script>
<script>
window.onload = function(){
lottie.loadAnimation({ //初始化
container: document.querySelector('.box'),//在哪個(gè)dom容器中生效
renderer: 'svg',//渲染方式svg
loop: true,//循環(huán)
autoplay: true,//自動(dòng)播放
animationData: JSON.parse(animationData),//動(dòng)畫數(shù)據(jù)
})
}
</script>到此這篇關(guān)于Javascript動(dòng)畫插件lottie-web的使用方法的文章就介紹到這了,更多相關(guān)Javascript動(dòng)畫插件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS小功能(列表頁(yè)面隔行變色)簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了JS列表頁(yè)面隔行變色簡(jiǎn)單實(shí)現(xiàn),有需要的朋友可以參考一下2013-11-11
淺談bootstrap源碼分析之tab(選項(xiàng)卡)
下面小編就為大家?guī)硪黄獪\談bootstrap源碼分析之tab(選項(xiàng)卡)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
JS實(shí)現(xiàn)HTML頁(yè)面中動(dòng)態(tài)顯示當(dāng)前時(shí)間完整示例
這篇文章主要介紹了JS實(shí)現(xiàn)HTML頁(yè)面中動(dòng)態(tài)顯示當(dāng)前時(shí)間,結(jié)合完整實(shí)例形式分析了JavaScript使用時(shí)間函數(shù)setTimeout及clearTimeout動(dòng)態(tài)顯示當(dāng)前時(shí)間相關(guān)操作技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2018-07-07
Web?Woker與主線程通信場(chǎng)景下對(duì)postMessage的簡(jiǎn)潔封裝詳解
這篇文章主要為大家介紹了Web?Woker與主線程通信場(chǎng)景下對(duì)postMessage的簡(jiǎn)潔封裝示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
一步一步封裝自己的HtmlHelper組件BootstrapHelper(三)
一步一步封裝自己的HtmlHelper組件:BootstrapHelper,系列文章第三篇,感興趣的小伙伴們可以參考一下2016-09-09
Radio 單選JS動(dòng)態(tài)添加的選項(xiàng)onchange事件無效的解決方法
radio 單選JS動(dòng)態(tài)添加的選項(xiàng),onchange事件無效。使用delegate()函數(shù)可以解決該問題,具體解決方案大家通過本文詳細(xì)了解下2016-12-12
JavaScript實(shí)現(xiàn)滑動(dòng)門效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)滑動(dòng)門效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
Extjs4實(shí)現(xiàn)兩個(gè)GridPanel之間數(shù)據(jù)拖拽功能具體方法
這篇文章主要介紹了Extjs4實(shí)現(xiàn)兩個(gè)GridPanel之間數(shù)據(jù)拖拽功能具體方法,有需要的朋友可以參考一下2013-11-11

