欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

原生JS實(shí)現(xiàn)加載進(jìn)度條

 更新時(shí)間:2021年10月19日 17:18:45   作者:aiguangyuan  
這篇文章主要為大家詳細(xì)介紹了原生JS實(shí)現(xiàn)加載進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文分享一個(gè)原生JS實(shí)現(xiàn)的動(dòng)態(tài)加載進(jìn)度條特效,效果如下:

實(shí)現(xiàn)的代碼如下:

<!DOCTYPE html>
<html>
 
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>原生JS實(shí)現(xiàn)加載進(jìn)度條</title>
    <style>
        #progressBox {
            width: 300px;
            height: 40px;
            border: 1px solid #C8C8C8;
            background: white;
            position: relative;
            margin: 0 auto;
            margin-top: 100px;
        }
 
        #progressBar {
            position: absolute;
            left: 0;
            top: 0;
            z-index: 2;
            height: 40px;
            width: 100%;
            line-height: 40px;
            color: white;
            text-align: center;
            font-size: 20px;
            font-weight: bold;
            font-family: Georgia;
            clip: rect(0px, 0, 40px, 0px);
            background: #00A1F5;
        }
 
        #progressText {
            position: absolute;
            left: 0;
            top: 0;
            z-index: 1;
            width: 100%;
            height: 40px;
            line-height: 40px;
            color: black;
            text-align: center;
            font-size: 20px;
            font-weight: bold;
            font-family: Georgia;
        }
    </style>
    <script>
        window.onload = function () {
            // 設(shè)定當(dāng)前起始狀態(tài)值,
            // 真實(shí)情況中用html5的onprogress和onload來(lái)完成
            // 還可以跟后臺(tái)配合,通過(guò)ajax實(shí)時(shí)的返回?cái)?shù)據(jù)
            var iNow = 0;
            // 設(shè)定定時(shí)器
            var timer = setInterval(function () {
                // 如果當(dāng)前的值為100
                if (iNow == 100) {
                    // 清除定時(shí)器
                    clearInterval(timer);
                }else {
                    // 將當(dāng)前狀態(tài)值累加1
                    iNow += 1;
                    // 調(diào)用執(zhí)行狀態(tài)的函數(shù),傳入狀態(tài)值
                    progressFn(iNow);
                }
 
            }, 30);
 
 
            function progressFn(cent) {
                // 獲取最外層的div
                var oDiv1 = document.getElementById('progressBox');
                // 獲取內(nèi)層進(jìn)度條的div
                var oDiv2 = document.getElementById('progressBar');
                // 獲取內(nèi)層文字發(fā)生變化時(shí)的div
                var oDiv3 = document.getElementById('progressText');
 
                // 獲取總進(jìn)度條的寬度
                var allWidth = parseInt(getStyle(oDiv1, 'width'));
 
                // 設(shè)定內(nèi)層兩個(gè)div的文字內(nèi)容一樣
                oDiv2.innerHTML = cent + '%';
                oDiv3.innerHTML = cent + '%';
 
                // 修改clip的的寬度值
                oDiv2.style.clip = 'rect(0px, ' + cent / 100 * allWidth + 'px, 40px, 0px)';
 
                // 獲取當(dāng)前元素的屬性值
                function getStyle(obj, attr) {
                    // 兼容IE
                    if (obj.currentStyle) {
                        return obj.currentStyle[attr];
                    }else {
                        // 第二個(gè)參數(shù)為false是通用的寫(xiě)法,目的是為了兼容老版本
                        return getComputedStyle(obj, false)[attr];
                    }
                }
            }
        };
    </script>
</head>
 
<body>
    <div id="progressBox">
        <div id="progressBar">0%</div>
        <!-- 設(shè)定第二個(gè)層以便當(dāng)進(jìn)度超過(guò)文字的時(shí)候,修改文字的顏色 -->
        <div id="progressText">0%</div>
    </div>
</body>
 
</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論