您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

浅析JS异步加载进度条_javascript技巧

2025/10/13 15:35:21发布15次查看
展现效果:
1) 当点击load的时候,模拟执行异步加载. 浏览器被遮挡. 进度条出现.
实现思路:
1.当用户点击load button执行异步请求. 调用方法 出现加载条
2.怎么实现进度条呢?
1) 在document.body 新增一个div.覆盖浏览器. 设置背景会灰色. z-index = 999. 加载的时候让用户无法修改界面值
2) 在document.body 新增一个动态的div.
代码实现:
main.html:
load
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);}}
总结:
1.可以将方法提出来. 对ajax请求重新封装一次. 实现调用ajax请求的时候自动条用进度条方法.
2.如果是angular的话. angular提供了方法监控http调用.监控http执行请求的时候调用进度条方法就行了.无需在每次执行 http调用.监控http执行请求的时候调用进度条方法就行了.无需在每次执行http的时候都去自己调用出现进度条的方法.
以上内容是小编给大家介绍的js异步加载进度条的相关内容,希望对大家有所帮助!
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product