首頁  >  文章  >  web前端  >  淺談jQuery+CSS實作前端網頁載入進度列的三種方式

淺談jQuery+CSS實作前端網頁載入進度列的三種方式

青灯夜游
青灯夜游轉載
2021-02-25 10:27:543181瀏覽

淺談jQuery+CSS實作前端網頁載入進度列的三種方式

【推薦教學:CSS影片教學

前端網頁載入進度列的實作有三種方式,看自己需求:

一、頂部進度條

#在html程式碼中間插入jq程式碼  執行動畫。頁面載入到哪部分,進度條就會對應的向前走多少

當全部載入完成後將loading進度條模組隱藏

<!DOCTYPE html>
<html>
<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">
    <script src="jquery.min.js"></script>
    <title>顶部进度条</title>
    <style>
        .loading {position: fixed;top: 0;left: 0; width: 100%; height: 100%; background-color: #fff;}
        .pic {width: 0;height: 10px;background-color: #c33;border-radius: 5px;}
    </style>
</head>
<body>
    <div>
        <div></div>
    </div>
    <header>
        <img src="http://file02.16sucai.com/d/file/2015/0128/8b0f093a8edea9f7e7458406f19098af.jpg" alt="">
        <img src="http://img12.3lian.com/gaoqing02/02/93/37.jpg" alt="">
    </header>
    <script>
        $(".loading .pic").animate({width: "20%"},100);
    </script>
    <section>
        <img src="http://g.hiphotos.baidu.com/zhidao/pic/item/c83d70cf3bc79f3d6e7bf85db8a1cd11738b29c0.jpg" alt="">
        <img src="http://big5.wallcoo.com/photograph/summer_feeling/images/%5Bwallcoo.com%5D_summer_feeling_234217.jpg" alt="">
    </section>
    <script>
        $(".loading .pic").animate({width: "40%"},100);
    </script>
    <section>
        <img src="http://cdn.duitang.com/uploads/item/201409/08/20140908130732_kVXzh.jpeg" alt="">
        <img src="http://file02.16sucai.com/d/file/2014/1006/e94e4f70870be76a018dff428306c5a3.jpg" alt="">
    </section>
    <script>
        $(".loading .pic").animate({width: "60%"},100);
    </script>
    <section>
        <img src="http://b-ssl.duitang.com/uploads/item/201703/01/20170301163305_sCd8j.gif" alt="">
    </section>
    <script>
        $(".loading .pic").animate({width: "80%"},100);
    </script>
    <footer>
        <img src="http://img.51ztzj.com/upload/image/20130417/201304172007_670x419.jpg" alt="">
        <img src="http://img0.imgtn.bdimg.com/it/u=1618007397,4183425847&fm=26&gp=0.jpg" alt="">
    </footer>
    <script>
        $(".loading .pic").animate({width: "100%"},100, function() {
            $(".loading").fadeOut();
        });
    </script>
</body>
</html>

二、直接在頁面插入關於載入的動態圖,頁面載入完再隱藏掉

關於圖片可以在這個網站找:https:// loading.io/  可以依照自己的需求調樣式

淺談jQuery+CSS實作前端網頁載入進度列的三種方式

#當然,如果想自己寫也可以,以下是博主自己用css3寫的一個小動畫

淺談jQuery+CSS實作前端網頁載入進度列的三種方式淺談jQuery+CSS實作前端網頁載入進度列的三種方式

#程式碼如下

#
<!DOCTYPE html>
<html>
<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>css3动画进度条</title>
    <style>
        .loading {position: fixed;top:0;left:0;width: 100%; height: 100%;background-color: #fff;}
        .pic {position: absolute;top: 0;bottom: 0;left: 0;right: 0;margin: auto;width: 100px;height:40px;}
        .pic i {float:left;margin: 0 2px;width: 6px;height: 30px;background-color: indigo;transform: scaleY(0.4);animation: load 1.2s infinite;}
        .pic i:nth-child(2){-webkit-animation-delay: 0.1s;animation-delay: 0.1s;}
        .pic i:nth-child(3){-webkit-animation-delay: 0.2s;animation-delay: 0.2s;}
        .pic i:nth-child(4){-webkit-animation-delay: 0.3s;animation-delay: 0.3s;}
        .pic i:nth-child(5){-webkit-animation-delay: 0.4s;animation-delay: 0.4s;}
        .pic i:nth-child(6){-webkit-animation-delay: 0.5s;animation-delay: 0.5s;}
        @-webkit-keyframes load {
            0%,40%,100%{-webkit-transform: scaleY(0.4); transform: scaleY(0.4)}
            20%{-webkit-transform: scaleY(1); transform: scaleY(1)}
        }
        @keyframes load {
            0%,40%,100%{-webkit-transform: scaleY(0.4); transform: scaleY(0.4)}
            20%{-webkit-transform: scaleY(1); transform: scaleY(1)}
        }
    </style>
    <script src="jquery.min.js"></script>
    <script>
        document.onreadystatechange = function() {  // 页面状态发生改变时触发
            if(document.readyState == "complete") {  // 页面加载完成时隐藏
                $(".loading").fadeOut();
            }
        }
    </script>
</head>
<body>
    <div>
        <div>
            <i></i>
            <i></i>
            <i></i>
            <i></i>
            <i></i>
            <i></i>
        </div>
    </div>
    <img src="http://file02.16sucai.com/d/file/2015/0128/8b0f093a8edea9f7e7458406f19098af.jpg" alt="">
    <img src="http://img12.3lian.com/gaoqing02/02/93/37.jpg" alt="">
    <img src="http://g.hiphotos.baidu.com/zhidao/pic/item/c83d70cf3bc79f3d6e7bf85db8a1cd11738b29c0.jpg" alt="">
    <img src="http://big5.wallcoo.com/photograph/summer_feeling/images/%5Bwallcoo.com%5D_summer_feeling_234217.jpg" alt="">
    <img src="http://cdn.duitang.com/uploads/item/201409/08/20140908130732_kVXzh.jpeg" alt="">
    <img src="http://file02.16sucai.com/d/file/2014/1006/e94e4f70870be76a018dff428306c5a3.jpg" alt="">
    <img src="http://b-ssl.duitang.com/uploads/item/201703/01/20170301163305_sCd8j.gif" alt="">
    <img src="http://img.51ztzj.com/upload/image/20130417/201304172007_670x419.jpg" alt="">
    <img src="http://img0.imgtn.bdimg.com/it/u=1618007397,4183425847&fm=26&gp=0.jpg" alt="">
</body>
</html>

三、即時取得資料的進度列(百分比)

淺談jQuery+CSS實作前端網頁載入進度列的三種方式

#程式碼如下

<!DOCTYPE html>
<html>
<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 {position: fixed;top:0;left:0;width: 100%; height: 100%;background-color: #fff;}
        .pic {position: absolute;top: 0;bottom: 0;left: 0;right: 0;margin: auto;width: 100px;height:100px;line-height: 55px;text-align: center;font-size: 22px;}
        .pic span {display: inline-block;position: absolute;top: 10px;left: 10px;width: 80px;height: 80px;border-radius: 50%;box-shadow: 0 3px 0 #999;
                -webkit-animation: move 1s infinite linear;animation: move 1s infinite linear;}
        @-webkit-keyframes move {
            0%{-webkit-transform: rotate(0deg);transform: rotate(0deg);}
            100%{-webkit-transform: rotate(360deg);transform: rotate(360deg);}
        }
        @keyframes move {
            0%{-webkit-transform: rotate(0deg);transform: rotate(0deg);}
            100%{-webkit-transform: rotate(360deg);transform: rotate(360deg);}
        }
    </style>
    <script src="jquery.min.js"></script>
    <script>
        $(function() {
            var img = $("img"); // 获取所有的img元素
            var num = 0;  // 用来存储已加载的图片个数
            img.each(function(i) {  // 遍历所有图片
                var oImg = new Image();
                oImg.onload = function() { // onload 图片加载完成后执行
                    num++;
                    $(".pic p").html(parseInt(num / img.length * 100) + &#39;%&#39;);
                    if(num >= img.length) {
                        $(".loading").fadeOut(); // 页面加载完成后隐藏
                    }
                }
                oImg.src = img[i].src;
            })
        })
    </script>
</head>
<body>
    <div>
        <div>
            <span></span>
            <p>0%</p>
        </div>
    </div>
    <img src="http://file02.16sucai.com/d/file/2015/0128/8b0f093a8edea9f7e7458406f19098af.jpg" alt="">
    <img src="http://img12.3lian.com/gaoqing02/02/93/37.jpg" alt="">
    <img src="http://g.hiphotos.baidu.com/zhidao/pic/item/c83d70cf3bc79f3d6e7bf85db8a1cd11738b29c0.jpg" alt="">
    <img src="http://big5.wallcoo.com/photograph/summer_feeling/images/%5Bwallcoo.com%5D_summer_feeling_234217.jpg" alt="">
    <img src="http://cdn.duitang.com/uploads/item/201409/08/20140908130732_kVXzh.jpeg" alt="">
    <img src="http://file02.16sucai.com/d/file/2014/1006/e94e4f70870be76a018dff428306c5a3.jpg" alt="">
    <img src="http://b-ssl.duitang.com/uploads/item/201703/01/20170301163305_sCd8j.gif" alt="">
    <img src="http://img.51ztzj.com/upload/image/20130417/201304172007_670x419.jpg" alt="">
    <img src="http://img0.imgtn.bdimg.com/it/u=1618007397,4183425847&fm=26&gp=0.jpg" alt="">
</body>
</html>

更多程式相關知識,請造訪:程式設計影片! !

以上是淺談jQuery+CSS實作前端網頁載入進度列的三種方式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除