是否可以适配以下jQuery?
Is it possible to adapt the following jQuery?
我最近在我的网站上添加了一个在页面加载时响应式加载图标。除了我想在执行 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>
有没有办法修改 jQuery 以使其更适合我?或者你们有完全不同的解决方案仍然可以解决问题吗?
一些像https://github.com/jgerigmeyer/jquery-loading-overlay这样的轻量插件(2kb)存在并且可以像这样使用:
开始:
$('#target').loadingOverlay();
并停止:
$('#target').loadingOverlay('remove');
只要把它放在你的函数中,不要忘记做一个同步调用,否则它将毫无意义:)
如果我清楚地理解您的要求,那么我认为您希望在 xmlhttp 请求之前添加一个加载图像,当请求工作完成时它就会消失。
首先在html文件中添加以下代码
<div id="loadingDiv">
<div id="popupContact">
</div>
</div>
您 css 中的以下行
并且不要忘记在您的图像文件夹中添加一个 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;
}
然后在你的js文件中添加以下两个方法
//it will show process image
function showProcessImg(){
$('#loadingDiv').css('display','block');
}
//it will hide process image
function hideProcessImg(){
$('#loadingDiv').css('display','none');
}
在您的 httprequest 之前调用 showProcessImg() 并在您的 httprequest 成功或错误回调中调用 hideProcessImg() 如下
showProcessImg();
$.ajax({
url:'',
method:'POST',
success:function(response){
hideProcessImg();
},
error:function(){
//disable loading image
hideProcessImg();
alert('Error occur here');
}
});
只需创建一个叠加层 div
然后切换其 display
属性。
heres一个fiddle就可以了。请注意,不需要图像,但您需要 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;
}
如果您将此用于 AJAX。使用以下方式 show/hide loader.
$(".se-pre-con").show(); // before initiate ajax call
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$(".se-pre-con").hide();
});
此处给出的所有答案都很好,但如果您希望所有 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;
}
谢谢
我最近在我的网站上添加了一个在页面加载时响应式加载图标。除了我想在执行 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>
有没有办法修改 jQuery 以使其更适合我?或者你们有完全不同的解决方案仍然可以解决问题吗?
一些像https://github.com/jgerigmeyer/jquery-loading-overlay这样的轻量插件(2kb)存在并且可以像这样使用:
开始:
$('#target').loadingOverlay();
并停止:
$('#target').loadingOverlay('remove');
只要把它放在你的函数中,不要忘记做一个同步调用,否则它将毫无意义:)
如果我清楚地理解您的要求,那么我认为您希望在 xmlhttp 请求之前添加一个加载图像,当请求工作完成时它就会消失。
首先在html文件中添加以下代码
<div id="loadingDiv">
<div id="popupContact">
</div>
</div>
您 css 中的以下行
并且不要忘记在您的图像文件夹中添加一个 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;
}
然后在你的js文件中添加以下两个方法
//it will show process image
function showProcessImg(){
$('#loadingDiv').css('display','block');
}
//it will hide process image
function hideProcessImg(){
$('#loadingDiv').css('display','none');
}
在您的 httprequest 之前调用 showProcessImg() 并在您的 httprequest 成功或错误回调中调用 hideProcessImg() 如下
showProcessImg();
$.ajax({
url:'',
method:'POST',
success:function(response){
hideProcessImg();
},
error:function(){
//disable loading image
hideProcessImg();
alert('Error occur here');
}
});
只需创建一个叠加层 div
然后切换其 display
属性。
heres一个fiddle就可以了。请注意,不需要图像,但您需要 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;
}
如果您将此用于 AJAX。使用以下方式 show/hide loader.
$(".se-pre-con").show(); // before initiate ajax call
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$(".se-pre-con").hide();
});
此处给出的所有答案都很好,但如果您希望所有 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;
}
谢谢