淺析JS異步加載進(jìn)度條
展現(xiàn)效果:
1) 當(dāng)點(diǎn)擊Load的時(shí)候,模擬執(zhí)行異步加載. 瀏覽器被遮擋. 進(jìn)度條出現(xiàn).

實(shí)現(xiàn)思路:
1.當(dāng)用戶點(diǎn)擊load button執(zhí)行異步請求. 調(diào)用方法 出現(xiàn)加載條
2.怎么實(shí)現(xiàn)進(jìn)度條呢?
1) 在document.body 新增一個(gè)div.覆蓋瀏覽器. 設(shè)置背景會灰色. z-index = 999. 加載的時(shí)候讓用戶無法修改界面值
2) 在document.body 新增一個(gè)動態(tài)的div.
代碼實(shí)現(xiàn):
Main.html:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="Loading.js" charset="utf-8"></script> <link rel="stylesheet" href="Loading.css" media="screen" title="no title" charset="utf-8"> </head> <body> <button onclick="showLoading()">Load</button> </body> </html>
Loading.js:
function showLoading()
{
var overDiv = document.createElement("div");
var loadingDiv = document.createElement("div");
var childDiv1 = document.createElement("div");
var childDiv2 = document.createElement("div");
var childDiv3 = document.createElement("div");
overDiv.classList.add('over');
loadingDiv.classList.add('spinner');
childDiv1.classList.add('bounce1')
childDiv2.classList.add('bounce2')
childDiv3.classList.add('bounce3')
loadingDiv.appendChild(childDiv1);
loadingDiv.appendChild(childDiv2);
loadingDiv.appendChild(childDiv3);
document.body.appendChild(overDiv);
document.body.appendChild(loadingDiv)
setTimeout(function()
{
document.body.removeChild(overDiv);
document.body.removeChild(loadingDiv)
}, 5000);
}
Loading.css
.spinner {
width: 150px;
text-align: center;
left: 50%;
top: 50%;
position: absolute;
z-index: 1000;
}
.over {
width: 100%;
height: 100%;
z-index: 998;
background-color: gray;
position:absolute;
top: 0px ;
left : 0px;
opacity: 0.2;
}
.spinner > div {
width: 30px;
height: 30px;
background-color: #67CF22;
border-radius: 100%;
display: inline-block;
-webkit-animation: bouncedelay 1.4s infinite ease-in-out;
animation: bouncedelay 1.4s infinite ease-in-out;
/* Prevent first frame from flickering when animation starts */
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
.spinner .bounce1 {
-webkit-animation-delay: -0.32s;
animation-delay: -0.32s;
}
.spinner .bounce2 {
-webkit-animation-delay: -0.16s;
animation-delay: -0.16s;
}
@-webkit-keyframes bouncedelay {
0%, 80%, 100% { -webkit-transform: scale(0.0) }
40% { -webkit-transform: scale(1.0) }
}
@keyframes bouncedelay {
0%, 80%, 100% {
transform: scale(0.0);
-webkit-transform: scale(0.0);
} 40% {
transform: scale(1.0);
-webkit-transform: scale(1.0);
}
}
總結(jié):
1.可以將方法提出來. 對Ajax請求重新封裝一次. 實(shí)現(xiàn)調(diào)用Ajax請求的時(shí)候自動條用進(jìn)度條方法.
2.如果是Angular的話. Angular提供了方法監(jiān)控http調(diào)用.監(jiān)控http執(zhí)行請求的時(shí)候調(diào)用進(jìn)度條方法就行了.無需在每次執(zhí)行 http調(diào)用.監(jiān)控http執(zhí)行請求的時(shí)候調(diào)用進(jìn)度條方法就行了.無需在每次執(zhí)行http的時(shí)候都去自己調(diào)用出現(xiàn)進(jìn)度條的方法.
以上內(nèi)容是小編給大家介紹的js異步加載進(jìn)度條的相關(guān)內(nèi)容,希望對大家有所幫助!
相關(guān)文章
uniapp?app?人臉識別的實(shí)現(xiàn)示例
本文主要介紹了uniapp?app?人臉識別的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
微信小程序頁面調(diào)用自定義組件內(nèi)的事件詳解
這篇文章主要介紹了微信小程序頁面調(diào)用自定義組件內(nèi)的事件詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
js獲取鼠標(biāo)點(diǎn)擊的對象,點(diǎn)擊另一個(gè)按鈕刪除該對象的實(shí)現(xiàn)代碼
下面小編就為大家?guī)硪黄猨s獲取鼠標(biāo)點(diǎn)擊的對象,點(diǎn)擊另一個(gè)按鈕刪除該對象的實(shí)現(xiàn)代碼。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05
js實(shí)現(xiàn)window.open不被攔截的解決方法匯總
這篇文章主要介紹了js實(shí)現(xiàn)window.open不被攔截的解決方法,實(shí)例匯總了常用的不被攔截的解決方法,需要的朋友可以參考下2014-10-10
Javascript Event事件中IE與標(biāo)準(zhǔn)DOM的比較
說是IE與DOM的比較,其實(shí)還是瀏覽器之間的比較,眾多的瀏覽器中,IE獨(dú)樹一幟,并且占有大部分用戶市場,后來的標(biāo)準(zhǔn)DOM雖然不少優(yōu)點(diǎn)之處,但畢竟一般用戶不關(guān)心也不知道這些,代碼編寫過程中,往往需要兼顧多個(gè)瀏覽器。2010-04-04
bootstrap實(shí)現(xiàn)動態(tài)進(jìn)度條效果
本篇文章主要介紹了bootstrap實(shí)現(xiàn)動態(tài)進(jìn)度條效果,進(jìn)度條可以加強(qiáng)應(yīng)用的用戶體驗(yàn)效果,看到數(shù)字,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03

