vue仿elementui自定义一个v-pageloading指令,开箱即用

news/2024/7/10 1:46:10 标签: vue, js
  • 创建一个loading文件夹。包含loading.vue、loading.js、index.js
  • loading.vue 写布局及css样式
  • 实例代码
<template>
    <div class="loading-container" v-show="loadingShow">
        <div class="pageloading">
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </div>
    </div>
</template>

<script>
    export default {
        data () {
            return {
                loadingShow: false,
            }
        }
    }
</script>

<style scoped>
    .loading-container{
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        padding-top:100px;
        z-index: 99999999;
        background: rgba(255, 255, 255, 0.7);
    }
    .pageloading{
        width: 40px;
        /* height: 50px; */
        margin: 0 auto;
    }
    .pageloading span{
        display: inline-block;
        width: 5px;
        height: 100%;
        border-radius: 4px;
        background: lightgreen;
        -webkit-animation: load 1s ease infinite;
        animation: load 1s ease infinite;
    }
    @-webkit-keyframes load{
        0%,100%{
            height: 20px;
            background: lightgreen;
        }
        50%{
            height: 50px;
            margin: -15px 0;
            background: lightblue;
        }
    }
    @keyframes load{
        0%,100%{
            height: 20px;
            background: lightgreen;
        }
        50%{
            height: 50px;
            margin: -15px 0;
            background: lightblue;
        }
    }
    .pageloading span:nth-child(2){
        -webkit-animation-delay:0.2s;
        animation-delay:0.2s;
    }
    .pageloading span:nth-child(3){
        -webkit-animation-delay:0.4s;
        animation-delay:0.4s;
    }
    .pageloading span:nth-child(4){
        -webkit-animation-delay:0.6s;
        animation-delay:0.6s;
    }
    .pageloading span:nth-child(5){
        -webkit-animation-delay:0.8s;
        animation-delay:0.8s;
    }
</style>

  • loading.js 显示隐藏逻辑处理
  • 实例代码
import Vue from 'vue'
import pageLoading from './loading.vue'

const Mask = Vue.extend(pageLoading)

const toggleLoading = (el, binding) => {
	//el拿到loading挂载的元素,binding.value判断是否显示loading
    if (binding.value) {
        Vue.nextTick(() => {
            el.instance.loadingShow = true
            insertDom(el, el, binding)
        })
    } else {
        el.instance.loadingShow = false
    }
}

const insertDom = (parent, el) => {
    parent.appendChild(el.mask)
}

export default {
    bind: function (el, binding) {
        const mask = new Mask({
            el: document.createElement('div'),
            data() { }
        })
        el.instance = mask
        el.mask = mask.$el
        el.maskStyle = {}
        binding.value && toggleLoading(el, binding)
    },
    update: function (el, binding) {
        if (binding.oldValue !== binding.value) {
            toggleLoading(el, binding)
        }
    },
    unbind: function (el, binding) {
        el.instance && el.instance.$destroy()
        el = null
    }
}
  • index.js 注册一个插件
  • 实例代码
import pageloading from './loading.js'

export default {
    install (Vue) {
        Vue.directive('pageloading', pageloading)
    }
}
  • 在main.js导入
import pageloading from './components/pageloading';
Vue.use(pageloading);
  • 使用方式
<template>
	<div class="container" v-pageloading="pageloading">page</div>
	<button @click="changeLoading">打开/关闭loading</button>
</template>
<script>
	export default{
		data(){
			return {
				pageloading:false,
			}
		},
		methods:{
			changeLoading(){
				this.pageloading = !this.pageloading;
			}
		}
	}
</script>
  • 最后附上一个大佬写的css loading样式 传送门

http://www.niftyadmin.cn/n/1364951.html

相关文章

ssh之雇员管理系统(8)-增加其他功能

一、增加雇员 前面我们已经讲得有大部分关键技术啦&#xff0c;而现在也就是我们要扩展&#xff0c;写出来自己的水平啦&#xff0c;当然&#xff0c;逻辑和你的经验将是成功的保障。 增加雇员的话&#xff0c;我们先将这个页面打通<a href"${pageContext.request.cont…

vue2 elementui日期时间选择器

设置日期选择器&#xff08;范围&#xff09;只能选择当前月份的日期 <el-date-pickerv-model"search.date"type"daterange"range-separator"至"start-placeholder"开始日期"end-placeholder"结束日期"value-format&quo…

图的遍历(广度优先遍历)- 数据结构和算法61

图的遍历&#xff08;广度优先遍历&#xff09; 让编程改变世界 Change the world by program 广度优先遍历 广度优先遍历&#xff08;BreadthFirstSearch&#xff09;&#xff0c;又称为广度优先搜索&#xff0c;简称BFS。 如果以之前我们找钥匙的例子来讲&#xff0c;运用…

antdesign-vue table合并列

合并的方法 const temp {}; const mergeCells (text, array, columns) > {let rowSpan 0if (array.length 1) {rowSpan 1} else {if (text ! temp[columns]) {temp[columns] textarray.forEach((item, index, arr) > {if (item[columns] temp[columns]) {rowSpan…

Spring IOC原理解析

首先恭喜守宏同学找到了自己心仪的工作&#xff0c;入职的事情终于尘埃落定&#xff0c;也算是一个新的开始吧。和守宏聊天的时候也说了很多有关工作的事情&#xff0c;畅想了以后美好的未来&#xff0c;也想到了今后的种种困难。不说别的就是单单在北京住房这一项就够任何一个…

mPaaS小程序使用国密算法sm2加密传参

使用 miniprogram-sm-crypto使用文档npm i miniprogram-sm-crypto引入 const sm2 require(‘miniprogram-sm-crypto’).sm2; 在js文件里 const sm2 require(miniprogram-sm-crypto).sm2; const smTwo {sm2,cipherMode: 1, // 1 - C1C3C2&#xff0c;0 - C1C2C3&#xff0c;…

windows API编写的提取文件属性的程序

用VC6.0的编译环境&#xff0c;建的是win32 console application#include <stdafx.h>#include <string.h>#include <windows.h>DWORD ShowFileTime(PFILETIME lptime){ //文件时间结构 FILETIME ftLocal; //系统时间结构 SYSTEMTIME st; //调整为系统所在时区…

vue2 antdesign menu 渲染多级菜单

示例数据 //path 自行配置 const menu [{title: 示例1,icon: laptop,path: ,children: [{title: 示例1-1,icon: laptop,path: },{title: 示例1-2,icon: laptop,path: }]}{title: 示例2,icon: laptop,path: ,children: [{title: 示例2-1,icon: laptop,path: ,children: [{titl…