在页面未加载完之前显示loading动画

在页面未加载完之前显示loading动画

这里有很多比这篇博客还优秀的loading动画源码

我还参考这篇博客

loading动画代码demo

我的demo预览

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>use-pseudo-class</title>
    <style>
        .loading{
            width: 100px;
            height: 100px;
            border: 1px solid red;
            position: relative;
        }
        .loading::before,.loading::after{
            content: '';
            /*这里要加一个content,不然什么都没有*/
            position: absolute;
            width: 10px;
            height: 10px;
            background: #000;
            border-radius: 50%;

            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            animation: toBig 1.5s linear infinite;
        }
        .loading::after{
            animation-delay: 0.75s;
            /*background-color: red;*/
        }
        @keyframes toBig {
            0%{
                width: 0;
                height: 0;
                opacity: 1;
            }
            100%{
                width: 50px;
                height:50px;
                opacity: 0;
            }
        }
    </style>
</head>
<body>
<div class="loading">
</div>
</body>
</html>

加入到实际页面的使用方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>loading动画</title>

    <!--loading style-->
    <style>
        .loading{
            width: 100px;
            height: 100px;
            /*border: 1px solid red;*/
            position: relative;
        }
        .loading::before,.loading::after{
            content: '';
            /*这里要加一个content,不然什么都没有*/
            position: absolute;
            width: 0;
            height: 0;
            background: #000;
            border-radius: 50%;

            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            animation: toBig 1s linear infinite;
        }
        .loading::after{
            animation-delay: 0.5s;
            /*background-color: red;*/
        }
        @keyframes toBig {
            0%{
                width: 0;
                height: 0;
                opacity: 1;
            }
            100%{
                width: 50px;
                height:50px;
                opacity: 0;
            }
        }
    </style>
    <style>
        .site-welcome{
            display: none;
            justify-content:center;
            align-items:center;
            
            /*里面内容居中使用flex在父元素添加三行代码display:flex;justify-content:center;
            align-items:center;*/
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            /*上面四行代码,让这个fixed铺满整个画面*/
            background-color: #ccc;
            z-index: 1;
        }
        .site-welcome.active{
            display:flex;
        }
    </style>
</head>

<body>
    <div class="site-welcome active" id="siteWelcome">
        <div class="loading">

        </div>
    </div>
    
    //.
    //.
    //.
    //这里是其他代码
    
    <script>
       //当代码加载到这里的时候,执行这个script,当代码加载到这里,说明loading该结束了

        window.onload=function(){
            var siteWelcome = document.getElementById('siteWelcome');
            siteWelcome.classList.remove('active');
        }
    </script>
</body>
</html>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值