lottie實(shí)現(xiàn)vue自定義loading指令及常用指令封裝詳解
一、前言
本文主要介紹使用lottie動畫實(shí)現(xiàn)vue自定義loading的指令方法。另外本篇文章還會介紹其他幾個(gè)常用的自定義指令實(shí)現(xiàn)方式(點(diǎn)擊指定區(qū)域外監(jiān)聽指令)。
lottie是一款由airbnb開源的跨平臺動畫渲染庫,支持Android
,iOS
,Web
,Windows
平臺。是專門用于解析從AE(Adobe After Effects)中通過Bodymovin
插件導(dǎo)出的JSON文件,直接渲染動畫。
二、實(shí)現(xiàn)方式
1.v-loading
這個(gè)指令是我們在開發(fā)過程中經(jīng)常遇到的場景,這里我們分兩步做,第一步我們先創(chuàng)建一個(gè)loading的組件。
這里我們要先引入lottie
插件
npm install lottie-web // or yarn add lottie-web
之后在loading組件中引入
// loading.vue <template> <div class="loading-wrap"> <div id="loadingImg" /> </div> </template> <script> import lottie from "lottie-web" // 在網(wǎng)上下載的lottie動畫,這里我是在iconfont上找的 import animationData from "./loading.json" export default { mounted() { // 解決json動畫找不到dom不渲染問題 window.requestAnimationFrame(this.loadImg); }, methods: { loadImg () { lottie.loadAnimation({ container: document.getElementById('loadingImg'), renderer: "svg", loop: true, autoplay: true, animationData: animationData }); } } }; </script> <style lang="less" scoped> .loading-wrap { position: absolute; left: 0; right: 0; top: 0; bottom: 0; background-color: #fff; display: flex; align-items: center; justify-content: center; z-index: 1800; } </style>
然后去在創(chuàng)建一個(gè)js文件去寫指令
import Vue from 'vue' import Loading from './loading.vue' /** * Vue.extend 接受參數(shù)并返回一個(gè)構(gòu)造器,new 該構(gòu)造器可以返回一個(gè)組件實(shí)例 * 當(dāng)我們 new Mask() 的時(shí)候,把該組件實(shí)例掛載到一個(gè) div 上 **/ const Mask = Vue.extend(Loading) // 更新是否顯示 const toggleLoading = (el, binding) => { if (binding.value) { Vue.nextTick(() => { // 獲取父元素的定位信息 const isStatic = window.getComputedStyle(el).position === 'static' if (isStatic) { el.style.position = 'relative' } // 插入到目標(biāo)元素 insertDom(el, el, binding) }) } else { removeDom(el, el, binding) } } // 插入到目標(biāo)元素 const insertDom = (parent, el) => { parent.appendChild(el.mask) } // 從目標(biāo)元素中移除 const removeDom = (parent, el) => { parent.removeChild(el.mask) } export default { // 第一次綁定到元素時(shí)調(diào)用 bind: function (el, binding) { const mask = new Mask({ el: document.createElement('div') }) // 用一個(gè)變量接住mask實(shí)例 el.instance = mask el.mask = mask.$el el.maskStyle = {} binding.value && toggleLoading(el, binding) }, // 所在組件的 VNode 更新時(shí)調(diào)用--比較更新前后的值 update: function (el, binding) { if (binding.oldValue !== binding.value) { toggleLoading(el, binding) } }, // 指令與元素解綁時(shí)調(diào)用 unbind: function (el) { el.instance && el.instance.$destroy() } }
然后我們再去main.js中,全局注冊一下
import Directive from './directives' Vue.use(Directive)
最后,在使用的時(shí)候,就可以這樣
// App.vue <template> <div id="app" v-loading="isLoading"> </div> </template> <script> export default { name: 'App', data () { return { isLoading: true } }, mounted () { setTimeout(() => { this.isLoading = false }, 3000) } } </script>
以上 我們就封裝好了一個(gè)lottie的自定義loading。
2.v-click-outside
這個(gè)指令是點(diǎn)擊目標(biāo)區(qū)域以外的地方的監(jiān)聽。在日常開發(fā)中,我們可能會遇到自定義的一些彈窗效果,這個(gè)時(shí)候,這個(gè)指令就派上用場了。
// clickoutside.js export default { bind (el, binding, vnode) { function documentHandler (e) { if (el.contains(e.target)) { return false } if (binding.expression) { binding.value(e) } } el.__vueClickOutside__ = documentHandler document.addEventListener('click', documentHandler) }, update () { }, unbind (el, binding) { document.removeEventListener('click', el.__vueClickOutside__) delete el.__vueClickOutside__ } }
之后是使用方法, 如下
<template> <div id="app"> <img v-click-outside="handleOutSide" src="./assets/logo.png" > </div> </template> <script> export default { name: 'App', methods: { handleOutSide () { console.log('到外面了') } } } </script>
最后記得在index.js中將兩個(gè)指令定義一下,再去main.js中注冊到全局。
import Loading from './loading' import clickoutside from './clickoutside' export default { install (Vue) { Vue.directive('loading', Loading), Vue.directive('click-outside', clickoutside) } }
目錄結(jié)構(gòu)如圖
以上我們就實(shí)現(xiàn)了常用指令的自定義。
三、后記
關(guān)于自定義指令有非常多,比如權(quán)限時(shí)候可能會用到、占位圖、loading等。我們都可以用指令的封裝,快速便捷的實(shí)現(xiàn)一些功能。
以上就是lottie實(shí)現(xiàn)vue自定義loading指令及常用指令封裝的詳細(xì)內(nèi)容,更多關(guān)于lottie vue自定義loading指令的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在vue中封裝的彈窗組件使用隊(duì)列模式實(shí)現(xiàn)方法
這篇文章主要介紹了在vue中封裝的彈窗組件使用隊(duì)列模式實(shí)現(xiàn)方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07使用Vue自定義數(shù)字鍵盤組件(體驗(yàn)度極好)
最近做 Vue 開發(fā),因?yàn)橛胁簧夙撁嫔婕暗浇痤~輸入,產(chǎn)品老是覺得用原生的 input 進(jìn)行金額輸入的話 體驗(yàn)很不好,于是自己動手寫了一個(gè)使用Vue自定義數(shù)字鍵盤組件,具體實(shí)現(xiàn)代碼大家參考下本文2017-12-12Vue中使用fetch讀取本地txt文件的技術(shù)實(shí)現(xiàn)
在Vue.js應(yīng)用開發(fā)中,有時(shí)我們需要從本地讀取文本文件(如 .txt 文件)并將其內(nèi)容展示在頁面上,這種需求在處理配置文件、日志文件或靜態(tài)數(shù)據(jù)時(shí)非常常見,本文將詳細(xì)介紹如何在Vue中使用 fetch API 讀取本地 .txt 文件,并提供多個(gè)示例和使用技巧2024-10-10使用vue 國際化i18n 實(shí)現(xiàn)多實(shí)現(xiàn)語言切換功能
這篇文章主要介紹了使用vue 國際化i18n 多實(shí)現(xiàn)語言切換功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-10-10vue組件實(shí)現(xiàn)移動端九宮格轉(zhuǎn)盤抽獎(jiǎng)
這篇文章主要為大家詳細(xì)介紹了vue組件實(shí)現(xiàn)移動端九宮格轉(zhuǎn)盤抽獎(jiǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10Vue報(bào)錯(cuò)"Failed?to?resolve?loader:less-loader"的解決方
這篇文章主要給大家介紹了關(guān)于Vue報(bào)錯(cuò)"Failed?to?resolve?loader:less-loader"的解決方法,文中通過圖文介紹的非常詳細(xì),對同樣遇到這樣問題的朋友具有一定的需要的朋友可以參考下2023-02-02