[英]Is it possible to adapt the following jQuery?
I've recently added to my website, a responsive loading icon while the page loads. 我最近在网页上添加了一个响应式加载图标到我的网站。 Except I would like to show the image whilst a JavaScript function is being executed because if the user runs the script a second time before the first xmlhttp request is finished, it wont work properly.
除非我想在执行JavaScript函数时显示图像,否则如果用户在第一个xmlhttp请求完成之前第二次运行脚本,它将无法正常工作。
$(window).load(function() { $(".se-pre-con").fadeOut("slow"); });
.no-js #loader { display: none; } .js #loader { display: block; position: absolute; left: 100px; top: 0; } .se-pre-con { position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; z-index: 9999; background: url(images/loader-64x/Preloader_2.gif) center no-repeat #fff; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="se-pre-con"></div>
Is there a way of modifying the jQuery to work better for me? 有没有一种方法可以修改jQuery以使其更适合我? or do you guys have a completely different solution that will still solve the problem?
还是你们有完全不同的解决方案,仍然可以解决问题?
If you are using this for AJAX. 如果您将此用于AJAX。 Use following way to show/hide loader.
使用以下方式显示/隐藏加载程序。
$(".se-pre-con").show(); // before initiate ajax call
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$(".se-pre-con").hide();
});
Some light plugin (2kb) like https://github.com/jgerigmeyer/jquery-loading-overlay exist and can be used like this : 存在一些轻量级插件(2kb),例如https://github.com/jgerigmeyer/jquery-loading-overlay ,可以这样使用:
to start : 开始 :
$('#target').loadingOverlay();
and to stop : 并停止:
$('#target').loadingOverlay('remove');
Just put it in your function and don't forget to do a synchronous call or it will mean nothing :) 只需将其放在函数中,别忘了进行同步调用 ,否则将毫无意义:)
If I am understanding your requirements clearly then I think that You want to add a loading image before your xmlhttp request and when request work complete then it goes off. 如果我清楚地理解了您的需求,那么我认为您想在xmlhttp请求之前添加一个加载图像,当请求工作完成时,它就会关闭。
first add below code in html file 首先在html文件中添加以下代码
<div id="loadingDiv">
<div id="popupContact">
</div>
</div>
following lines in your css and don't forget to add a bg.png
image in your images folder which you want for your loading image 在css中跟随几行,别忘了在要加载图像的images文件夹中添加
bg.png
图像
#loadingDiv {
width: 100%;
height: 100%;
top: 0;
left: 0;
display: none;
position: fixed;
background: url(../images/bg.png) repeat;
overflow: auto;
z-index: 100000;
}
div#popupContact {
position: fixed;
left: 48%;
top: 40%;
background: url(../images/loading.gif) no-repeat;
width: 100%;
height: 100%;
float: left;
}
then add following two methods in your js file 然后在您的js文件中添加以下两种方法
//it will show process image
function showProcessImg(){
$('#loadingDiv').css('display','block');
}
//it will hide process image
function hideProcessImg(){
$('#loadingDiv').css('display','none');
}
call to showProcessImg() before your httprequest and call hideProcessImg() in your success or error call back of httprequest as following 在您的httprequest之前调用showProcessImg(),并在成功或错误时调用hideProcessImg(),如下所示回调httprequest
showProcessImg();
$.ajax({
url:'',
method:'POST',
success:function(response){
hideProcessImg();
},
error:function(){
//disable loading image
hideProcessImg();
alert('Error occur here');
}
});
just create an overlay div
and then toggle its display
property. 只需创建一个叠加
div
,然后切换其display
属性即可。
heres a fiddle for it. 这是一件小事。 Note no image needed, but youll need a css3 supporting browser.
请注意,不需要图像,但需要使用支持CSS3的浏览器。
#waiting-container {
position: fixed;
width: 100%;
height: 100%;
z-index: 1130;
display: hide;
}
#waiting-container #waiting-spinner {
position: absolute;
width: 100%;
height: 100%;
z-index: 1132;
}
#waiting-spinner .spinner {
position: absolute;
left: 50%;
top: 50%;
height: 40px;
width: 40px;
margin: 0 auto;
-webkit-animation: rotation .6s infinite linear;
-moz-animation: rotation .6s infinite linear;
-o-animation: rotation .6s infinite linear;
animation: rotation .6s infinite linear;
border-left: 6px solid rgba(0, 174, 239, .15);
border-right: 6px solid rgba(0, 174, 239, .15);
border-bottom: 6px solid rgba(0, 174, 239, .15);
border-top: 6px solid rgba(0, 174, 239, .8);
border-radius: 100%;
}
@-webkit-keyframes rotation {
from {
-webkit-transform: rotate(0deg);
}
to {
-webkit-transform: rotate(359deg);
}
}
@-moz-keyframes rotation {
from {
-moz-transform: rotate(0deg);
}
to {
-moz-transform: rotate(359deg);
}
}
@-o-keyframes rotation {
from {
-o-transform: rotate(0deg);
}
to {
-o-transform: rotate(359deg);
}
}
@keyframes rotation {
from {
transform: rotate(0deg);
}
to {
transform: rotate(359deg);
}
}
#waiting-container #waiting-overlay {
position: absolute;
width: 100%;
height: 100%;
background: black;
opacity: .5;
z-index: 1131;
}
All the answers given here are good, but this is what you can try if you want the loader image by default for all AJAX requests: 这里给出的所有答案都是好的,但是如果您希望默认情况下所有AJAX请求的加载程序映像,可以尝试以下方法:
<body> </body> <div class="modalLoader"></div>
<script> $body = $("body"); $(document).on({ ajaxStart: function() { $body.addClass("loading");}, ajaxStop: function() { $body.removeClass("loading");} }); </script>
.modalLoader { display: none; position: fixed; z-index: 1500; top: 0; left: 0; height: 100%; width: 100%; background: rgba( 255, 255, 255, .8 ) url('../img/loaders/loader.gif') 50% 50% no-repeat; } body.loading { overflow: hidden; } body.loading .modalLoader { display: block; }
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.