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%', //如果父元素有傳參則使用傳參的值,沒(méi)有則=40%
height: this.height ? `${this.height}px` : '100%',//如果父元素有傳參則使用傳參的值,沒(méi)有則=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è)容器,通過(guò)空中這個(gè)容器的display屬性來(lái)控制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)行獲取,要留意跨域問(wèn)題。
為了避免跨域或者本地讀取時(shí)的權(quán)限問(wèn)題,可以講json文件的數(shù)據(jù)用 反引號(hào) `` 包裹起來(lái),用一個(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)文章
JavaScript使用指針操作實(shí)現(xiàn)約瑟夫問(wèn)題實(shí)例
這篇文章主要介紹了JavaScript使用指針操作實(shí)現(xiàn)約瑟夫問(wèn)題,實(shí)例分析了javascript模擬數(shù)組指針操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
JavaScript獲取網(wǎng)頁(yè)中第一個(gè)圖片id的方法
這篇文章主要介紹了JavaScript獲取網(wǎng)頁(yè)中第一個(gè)圖片id的方法,涉及javascript中document.images方法的使用技巧,需要的朋友可以參考下2015-04-04
前端頁(yè)面在移動(dòng)設(shè)備上顯示不正常的原因及解決方案
當(dāng)頁(yè)面在移動(dòng)設(shè)備上顯示不正常時(shí),通常是由于布局、樣式或響應(yīng)式設(shè)計(jì)設(shè)置不當(dāng)所引起的,移動(dòng)設(shè)備的屏幕尺寸、分辨率和交互方式與桌面設(shè)備有很大的不同,本文將詳細(xì)介紹常見的導(dǎo)致頁(yè)面在移動(dòng)設(shè)備上顯示不正常的原因,以及如何解決這些問(wèn)題,需要的朋友可以參考下2024-09-09
excel操作之Add Data to a Spreadsheet Cell
excel操作之Add Data to a Spreadsheet Cell...2007-06-06
javascript開發(fā)實(shí)現(xiàn)貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了javascript開發(fā)實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
TypeScript調(diào)整數(shù)組元素順序算法
數(shù)組類型在TS中可以使用多種方式,比較靈活,下面這篇文章主要給大家介紹了關(guān)于TypeScript調(diào)整數(shù)組元素順序算法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
解析從小程序開發(fā)者工具源碼看原理實(shí)現(xiàn)
小程序的架構(gòu)設(shè)計(jì)與web技術(shù)還是有一定的差別,其吸取了web技術(shù)的一些優(yōu)勢(shì),同時(shí)也摒棄web技術(shù)中體驗(yàn)等不好的地方。下面通過(guò)問(wèn)題的形式來(lái)說(shuō)說(shuō)小程序架構(gòu)中的一些設(shè)計(jì)點(diǎn)2021-06-06
詳解JS實(shí)現(xiàn)簡(jiǎn)單的時(shí)分秒倒計(jì)時(shí)代碼
這篇文章主要介紹了JS時(shí)分秒倒計(jì)時(shí)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

