vue 全局封装loading
这里提供两个不同样式的封装案例,具体如下:
案例一:动画实现
loading.vue
<template> <div v-if="show" class="lds-spinner"> <div class="spinner"> <div class="double-bounce1"></div> <div class="double-bounce2"></div> </div> </div> </template> <script> export default { name: 'Loading', props: { show: Boolean }, data() { return { } } } </script> <style lang="scss" scoped> .lds-spinner{ position: fixed; top:0; left:0; width:100%; height:100%; } .spinner { width: 25px; height: 25px; position:absolute; top:50%; left:50%; transform: translate(-50%); .double-bounce1, .double-bounce2 { width: 25px; height: 25px; border-radius: 50%; background-color: #FF000F; opacity: 0.6; position: absolute; top: 0; left: 0; -webkit-animation: bounce 2.0s infinite ease-in-out; animation: bounce 2.0s infinite ease-in-out; } .double-bounce2 { -webkit-animation-delay: -1.0s; animation-delay: -1.0s; } } @-webkit-keyframes bounce { 0%, 100% { -webkit-transform: scale(0.0) } 50% { -webkit-transform: scale(1.0) } } @keyframes bounce { 0%, 100% { transform: scale(0.0); -webkit-transform: scale(0.0); } 50% { transform: scale(1.0); -webkit-transform: scale(1.0); } } </style>
loading.js
import Vue from 'vue' import loadingComponent from '@/components/loading.vue' const LoadingConstructor = Vue.extend(loadingComponent) const instance = new LoadingConstructor({ el: document.createElement('div') }) instance.show = false // 默认隐藏 const loading = { show() { // 显示方法 instance.show = true document.body.appendChild(instance.$el) }, hide() { // 隐藏方法 instance.show = false } } export default { install() { if (!Vue.$loading) { Vue.$loading = loading } Vue.mixin({ created() { this.$loading = Vue.$loading } }) } }
main.js
import loading from './utils/loading.js' // 引入loading Vue.use(loading) // 全局使用loading
loading显示效果:
(此处按帧截图,效果不太明显,可复制代码直接看效果?)
案例二:图片+动画实现
loading.vue
<template> <div v-if="show" class="loading-container"> <div class="loading-mask"></div> <div class="loading-content"> <div class="loading-animate"></div> <div class="loading-text">{{ text }}</div> </div> </div> </template> <script> export default { name: 'Loading', props: { show: Boolean }, data() { return { text: '正在加载...' } } } </script> <style lang="scss" scoped> .loading-container{ position: relative; text-align: center; .loading-mask{ position: fixed; top:0; bottom:0; left:0; right:0; background-color:rgba(0,0,0,.7); } .loading-content{ position: fixed; left: 50%; top: 45%; z-index: 300; transform: translate(-50%,-45%); text-align: center; color:#fff; .loading-animate{ display: inline-block; width:35px; height:35px; margin: 25px 0 10px; vertical-align: middle; animation: cricleLoading 1s steps(12, end) infinite; background: transparent url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMjAiIGhlaWdodD0iMTIwIiB2aWV3Qm94PSIwIDAgMTAwIDEwMCI+PHBhdGggZmlsbD0ibm9uZSIgZD0iTTAgMGgxMDB2MTAwSDB6Ii8+PHJlY3Qgd2lkdGg9IjciIGhlaWdodD0iMjAiIHg9IjQ2LjUiIHk9IjQwIiBmaWxsPSIjRTlFOUU5IiByeD0iNSIgcnk9IjUiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDAgLTMwKSIvPjxyZWN0IHdpZHRoPSI3IiBoZWlnaHQ9IjIwIiB4PSI0Ni41IiB5PSI0MCIgZmlsbD0iIzk4OTY5NyIgcng9IjUiIHJ5PSI1IiB0cmFuc2Zvcm09InJvdGF0ZSgzMCAxMDUuOTggNjUpIi8+PHJlY3Qgd2lkdGg9IjciIGhlaWdodD0iMjAiIHg9IjQ2LjUiIHk9IjQwIiBmaWxsPSIjOUI5OTlBIiByeD0iNSIgcnk9IjUiIHRyYW5zZm9ybT0icm90YXRlKDYwIDc1Ljk4IDY1KSIvPjxyZWN0IHdpZHRoPSI3IiBoZWlnaHQ9IjIwIiB4PSI0Ni41IiB5PSI0MCIgZmlsbD0iI0EzQTFBMiIgcng9IjUiIHJ5PSI1IiB0cmFuc2Zvcm09InJvdGF0ZSg5MCA2NSA2NSkiLz48cmVjdCB3aWR0aD0iNyIgaGVpZ2h0PSIyMCIgeD0iNDYuNSIgeT0iNDAiIGZpbGw9IiNBQkE5QUEiIHJ4PSI1IiByeT0iNSIgdHJhbnNmb3JtPSJyb3RhdGUoMT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-size:100%; } .loading-text{ font-size: 14px; color: #fff; } } } @-webkit-keyframes cricleLoading { 0% { transform: rotate3d(0,0,1,0deg); } 100% { transform: rotate3d(0,0,1,360deg); } } @keyframes cricleLoading { 0% { transform: rotate3d(0,0,1,0deg); } 100% { transform: rotate3d(0,0,1,360deg); } } </style>
loading.js
// 代码同案例一的loading.js代码一致
- 1
main.js
// 引入方式也同案例一中main.js代码一致
- 1
在其他组件中使用loading:
// 同案例一中使用方式相同
- 1
loading显示效果: