美文网首页
页面加载小案例

页面加载小案例

作者: boyiis | 来源:发表于2018-11-08 11:19 被阅读0次

    将下面代码直接拷贝到.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>定时器加载进度条</title>
        <style>
            .loading {
                width: 100%;
                height: 100%;
                position: fixed;
                top: 0;
                left: 0;
                z-index: 100;
                background: #ffffff;
            }
    
            .loading .pic {
                width: 50px;
                height: 50px;
                position: absolute;
                top: 0;
                left: 0;
                bottom: 0;
                right: 0;
                margin: auto;
            }
            .loading .pic i{
                display: block;
                float: left;
                width: 6px;
                height: 50px;
                background: #399;
                margin: 0 2px;
                transform: scaleY(0.4);
                animation: load 1.2s infinite;
            }
    
            .loading .pic i:nth-child(1){}
            .loading .pic i:nth-child(2){animation-delay: 0.1s}
            .loading .pic i:nth-child(3){animation-delay: 0.2s}
            .loading .pic i:nth-child(4){animation-delay: 0.3s}
            .loading .pic i:nth-child(5){animation-delay: 0.4s}
    
            @keyframes load{
                0%, 40%, 100%{
                    transform: scaleY(0.4)
                }
                20% {
                    transform: scaleY(1)
                }
            }
        </style>
    </head>
    
    <body>
        <img src="http://h.hiphotos.baidu.com/image/pic/item/03087bf40ad162d92dc06e711cdfa9ec8a13cdb5.jpg" alt="">
        <img src="http://h.hiphotos.baidu.com/image/pic/item/d1160924ab18972b132c39acebcd7b899e510a45.jpg" alt="">
        <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1541652300993&di=1dd9e2513157ecd41aceed76bc74c970&imgtype=0&src=http%3A%2F%2Ft1.niutuku.com%2F960%2F30%2F30-416481.jpg"
            alt="">
    
        <div class="loading">
            <div class="pic">
                <i></i>
                <i></i>
                <i></i>
                <i></i>
                <i></i>
            </div>
        </div>
    
        <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
        <script>
            //实时加载
            document.onreadystatechange = function(){
                if("complete" === document.readyState){
                    $(".loading").fadeOut();
                }
            }
            //默认3秒隐藏
            $(function(){
                // setTimeout(function(){
                //     $(".loading").fadeOut();
                // }, 3000)
            })
    
        </script>
    </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>加载进度条</title>
        <style>
            * {
                padding: 0;
                margin: 0;
            }
    
            .loading {
                width: 100%;
                height: 100%;
                position: fixed;
                top: 0;
                left: 0;
                z-index: 100;
                background: #ffffff;
            }
    
            .loading .pic {
                width: 100px;
                height: 100px;
                position: absolute;
                top: 0;
                left: 0;
                bottom: 0;
                right: 0;
                margin: auto;
                font-size: 20px;
                text-align: center;
                line-height: 100px;
            }
    
            .loading .pic span {
                display: block;
                width: 80px;
                height: 80px;
                position: absolute;
                top: 10px;
                left: 10px;
                border-radius: 50%;
                box-shadow: 0 3px 0 #666666;
                animation: rotate 1s infinite linear;
            }
    
            @keyframes rotate {
                0% {
                    transform: rotate(0deg);
                }
                100% {
                    transform: rotate(360deg);
                }
            }
        </style>
    </head>
    
    <body>
    
        <header>
            <img src="http://h.hiphotos.baidu.com/image/pic/item/03087bf40ad162d92dc06e711cdfa9ec8a13cdb5.jpg" alt="">
        </header>
        <section class="banner">
            <img src="http://h.hiphotos.baidu.com/image/pic/item/d1160924ab18972b132c39acebcd7b899e510a45.jpg" alt="">
        </section>
        <section class="about">
            <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1541652300993&di=1dd9e2513157ecd41aceed76bc74c970&imgtype=0&src=http%3A%2F%2Ft1.niutuku.com%2F960%2F30%2F30-416481.jpg"
                alt="">
        </section>
        <section class="pro">
            <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1541652300993&di=1dd9e2513157ecd41aceed76bc74c970&imgtype=0&src=http%3A%2F%2Ft1.niutuku.com%2F960%2F30%2F30-416481.jpg"
                alt="">
        </section>
        <section class="news">
            <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1541652300993&di=1dd9e2513157ecd41aceed76bc74c970&imgtype=0&src=http%3A%2F%2Ft1.niutuku.com%2F960%2F30%2F30-416481.jpg"
                alt="">
        </section>
        <footer>
            <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1541652300993&di=1dd9e2513157ecd41aceed76bc74c970&imgtype=0&src=http%3A%2F%2Ft1.niutuku.com%2F960%2F30%2F30-416481.jpg"
                alt="">
        </footer>
    
        <div class="loading">
            <div class="pic">
                <span></span>
                <b>0%</b>
            </div>
        </div>
    
        <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
        <script>
     
            $(function () {
                var img = $("img");
                var num = 0;
    
                img.each(function(i){
                    var oImg = new Image();
                    oImg.onload = function(){
                        console.log(i);
                        console.log(num);
                        oImg.onload = null;
                        num ++;
                        console.log(i);
                        console.log(num);
                        $(".loading b").html(parseInt(num/$("img").size()*100) + "%");
                        
                        if(num >= i){
                            $(".loading").fadeOut();
                        }
                    }
                    oImg.src = img[i].src;
                });
    
                var img = new Image();
                
            })
    
        </script>
    </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>加载进度条</title>
        <script src="http://cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
        <script>    
            {
                function getElementsByClass(key) {
                    var arr = new Array();
                    var tags = document.getElementsByTagName("*");
                    for (var i = 0; i < tags.length; i++) {
                        if (tags[i].className.match(new RegExp('(\\s|^)' + key + '(\\s|$)'))) {
                            arr.push(tags[i]);
                        }
                    }
                    return arr;
                }
                var timer = window.setInterval(function () {
                    var el = getElementsByClass('pace');
                    if (el.length > 0) {
                        var a = document.createElement('div');
                        a.setAttribute('class', 'spinner');
                        var b = document.createElement('div');
                        b.setAttribute('class', 'spinner-icon');
                        el[0].appendChild(a);
                        a.appendChild(b);
                        window.clearInterval(timer);
                    }
                }, 3);
            }    
        </script>
        <style>
            .pace .spinner {
                position: fixed;
                top: 15px;
                right: 15px;
                z-index: 2000;
                display: block;
            }
    
            .pace .spinner-icon {
                width: 18px;
                height: 18px;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                border: solid 2px transparent;
                border-top-color: #2299dd;
                border-left-color: #2299dd;
                border-radius: 50%;
                -webkit-animation: nprogress-spinner .4s linear infinite;
                animation: nprogress-spinner .4s linear infinite;
            }
    
            @-webkit-keyframes nprogress-spinner {
                0% {
                    -webkit-transform: rotate(0deg);
                    transform: rotate(0deg);
                }
                100% {
                    -webkit-transform: rotate(360deg);
                    transform: rotate(360deg);
                }
            }
    
            @keyframes nprogress-spinner {
                0% {
                    -webkit-transform: rotate(0deg);
                    transform: rotate(0deg);
                }
                100% {
                    -webkit-transform: rotate(360deg);
                    transform: rotate(360deg);
                }
            }
    
            @keyframes fadeOut {
                from {
                    opacity: 1;
                }
                to {
                    opacity: 0;
                }
            }
    
            .pace {
                -webkit-pointer-events: none;
                pointer-events: none;
                -webkit-user-select: none;
                -moz-user-select: none;
                user-select: none;
                -webkit-transition: opacity 0.8s ease-in-out;
                -moz-transition: opacity 0.8s ease-in-out;
                -o-transition: opacity 0.8s ease-in-out;
                transition: opacity 0.8s ease-in-out;
            }
    
            .pace-inactive {
                opacity: 0;
                filter: alpha(opacity=0);
            }
    
            .pace .pace-progress {
                background: #2299dd;
                position: fixed;
                z-index: 2000;
                top: 0;
                right: 100%;
                width: 100%;
                height: 3px;
                box-shadow: 0 0 3px #2299dd;
            }
        </style>
        <!-- 右上角三角 
    <link href="http://cdn.bootcss.com/pace/1.0.2/themes/black/pace-theme-corner-indicator.min.css" rel="stylesheet">
    -->
    </head>
    
    <body>
    
        <header>
            <img src="http://h.hiphotos.baidu.com/image/pic/item/03087bf40ad162d92dc06e711cdfa9ec8a13cdb5.jpg" alt="">
    
        </header>
        <section class="banner">
            <img src="http://h.hiphotos.baidu.com/image/pic/item/d1160924ab18972b132c39acebcd7b899e510a45.jpg" alt="">
    
        </section>
        <section class="about">
            <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1541652300993&di=1dd9e2513157ecd41aceed76bc74c970&imgtype=0&src=http%3A%2F%2Ft1.niutuku.com%2F960%2F30%2F30-416481.jpg"
                alt="">
        </section>
        <section class="pro">
            <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1541652300993&di=1dd9e2513157ecd41aceed76bc74c970&imgtype=0&src=http%3A%2F%2Ft1.niutuku.com%2F960%2F30%2F30-416481.jpg"
                alt="">
        </section>
        <section class="news">
            <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1541652300993&di=1dd9e2513157ecd41aceed76bc74c970&imgtype=0&src=http%3A%2F%2Ft1.niutuku.com%2F960%2F30%2F30-416481.jpg"
                alt="">
        </section>
        <footer>
            <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1541652300993&di=1dd9e2513157ecd41aceed76bc74c970&imgtype=0&src=http%3A%2F%2Ft1.niutuku.com%2F960%2F30%2F30-416481.jpg"
                alt="">
        </footer>
    
    </body>
    
    </html>
    

    相关文章

      网友评论

          本文标题:页面加载小案例

          本文链接:https://www.haomeiwen.com/subject/wmnixqtx.html