在页面未加载完之前显示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>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,251评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,667评论 4 61
  • 写本周的读书笔记时,正在外面出差。但该做的事情还是得做的。本周读完了大名鼎鼎的《偷书贼》。 故事的背景...
    sophie_luo阅读 2,875评论 0 0
  • 关于这本《庆熹纪事》,能说道的事情不只是一二,先说它的网文出道,主角还是个太监,有人就要对它淡了一二分兴致,就像看...
    向沐然阅读 2,812评论 0 0
  • 亲爱的波比: 我又坐在灯下,给你写信。四周安静极了,除了这个季节的风,在街上、在屋顶、在窗边啸聚一下,把一些落叶和...
    小久_ab87阅读 1,598评论 0 1

友情链接更多精彩内容