利用ES6中的类制作一个简单Loading遮罩层

利用ES6中的类制作一个简单Loading遮罩层

  这是一个使用ES6的类,制作的一个简单的Loading遮罩层,可以展示Loading动画,或显示指定的文本,还可以设置遮罩层多少毫秒以后关闭,或者手动关闭遮罩层。
在这里插入图片描述
在这里插入图片描述
代码如下:

class Info{
    // 构造函数
    constructor(settings){
        this.configure = settings;
        this.rootNode = null;
        this.init();
    }
    
    // 初始化函数
    init(){
        let body = document.querySelector("body");
        body.insertAdjacentHTML("beforeend", "<div id='mask'></div>");
        this.rootNode = document.getElementById("mask");
    }

    // 判断是否有定时器
    show(){
        if(this.configure.time){
            this.maskShow();
            let that = this;
            setTimeout(function(){
                that.disappear();
            }, that.configure.time);
        }else{
            this.maskShow();
        }
    }
    
    // 调用maskShow()函数显示遮罩层
    maskShow(){
        this.rootNode.classList.add("mask");
        this.rootNode.style.display = "flex";

        // 判断显示Loading动画还是显示指定的文本
        if(this.configure.isLoading){
            this.rootNode.insertAdjacentHTML("beforeend", "<div id='mask-animation'></div>");
            let maksAnimation = document.getElementById("mask-animation");
            maksAnimation.insertAdjacentHTML("beforeend", `
                <span></span>
                <span></span>
                <span></span>
                <span></span>
                <span></span>
            `);
        }else{
            // this.rootNode.style.background = "none";
            this.rootNode.insertAdjacentHTML("beforeend", "<p id='mask-info'>"+ this.configure.text +"</p>");
        }
    }

    // 调用disappear()函数隐藏遮罩层
    disappear(){
        this.rootNode.style.display = "none";
        this.rootNode.classList.remove("mask");
    }
}
.mask{
    width: 100%;
    height: 100vh;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 99999999;
    background-color: rgba(255, 255, 255, 0.9);
    display: flex;
    justify-content: center;
    align-items: center;
}
#mask-info{
    width: 400px;
    height: 200px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #fab1a0;
    color: white;
    border-radius: 5px;
    font-size: 1.5rem;
    user-select: none;
    position: absolute;
    transform: translateY(-80vh);
    animation: down 1s ease 0s 1 forwards;
}
#mask-animation{
    width: 12rem;
    height: 12rem;
    display: flex;
    justify-content: center;
    align-items: center;
}
span{
    display: block;
    width: 15px;
    height: 60px;
    background-color: #1abc9c;
    margin: 0 5px;
}
span:nth-child(1){
    animation: up2 0.8s linear  infinite alternate-reverse;
}
span:nth-child(2){
    animation: up 0.8s linear  infinite alternate-reverse;
    animation-delay: 0.3s;
}
span:nth-child(3){
    animation: up 0.8s linear  infinite alternate-reverse;
    animation-delay: 0.4s;
}
span:nth-child(4){
    animation: up 0.8s linear  infinite alternate-reverse;
    animation-delay: 0.3s;
}
span:nth-child(5){
    animation: up2 0.8s linear  infinite alternate-reverse;
}

@keyframes up{
    0%{
        height: 30px;
    }
    100%{
        height: 120px;
    }
}
@keyframes up2{
    0%{
        height: 30px;
    }
    100%{
        height: 60px;
    }
}

@keyframes down{
    to{
        transform: translateY(0);
    }
}

在HTML中调用:

// 必须先引用上面的JS和CSS
<link rel="stylesheet" href="./mask.css">
<script src="./mask.js"></script>
<script>
    const info = new Info({
        isLoading: true, // 是否要显示动画
        // 为true时显示动画,为false时,显示文本
        text: "要显示的文本", // 如果不显示动画,则显示传入的文本
        // time: 2000, // 可以为空,如果不为空,则设置多少毫秒之后关闭遮罩层
    });
    info.show(); // 开启遮罩层
    // info.disappear(); // 关闭遮罩层
</script>

注:这个demo并未考虑移动端的兼容性,以及有些浏览器不支持ES6的类等情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值