網頁載入進度條中的javascript

來源:互聯網
上載者:User

標籤:javascript   進度條   

demo地址:http://output.jsbin.com/buquyedosa

思路如下:代碼都有注釋,就不一一介紹了。

<!DOCTYPE html><html><head lang="zh-cn">    <meta charset="UTF-8">    <title>進度條</title><style>    .progress{        position: fixed;        top: 0;        right: 0;        left: 0;        height: 20px;        background: #f5f5f5;        border-bottom: 1px solid #ddd;    }    .progress-inner{        width: 1%;        background: #abcdef;        position: absolute;        top: 0;        left: 0;        bottom: 0;    }</style></head><body onprogress=""><div class="progress">    <div class="progress-inner" id="progress"></div></div><script>    (function () {        // 擷取進度條 div        var $progress = document.getElementById(‘progress‘);        // 初始進度,1%        var progress = 1;        // 產生隨機數        var random = function(min, max){            return Math.floor(Math.random() * (max - min + 1) + min);        };        // 跑進度        var onprogress = function () {            // 隨機時間            var timeout = random(10, 30);            setTimeout(function () {                // 如果頁面載入完畢,則直接進度到 100%                if(window.loaded){                    $progress.style.width = ‘100%‘;                    return;                }                // 隨機進度                progress += random(1, 5);                // 隨機進度不能超過 98%,以免頁面還沒載入完畢,進度已經 100% 了                if(progress > 98){                    progress = 98;                }                $progress.style.width = progress + ‘%‘;                onprogress();            }, timeout);        };        // 開始跑進度        onprogress();        window.onload = function(){            window.loaded = true;        };    })();</script><iframe src="http://baidu.com/" frameborder="0"></iframe><iframe src="http://163.com/" frameborder="0"></iframe><iframe src="http://qq.com/" frameborder="0"></iframe><iframe src="http://tencent.com/" frameborder="0"></iframe></body></html>

網頁載入進度條中的javascript

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.