我想在我的网站上放置一个“请等待,正在加载”旋转的圆圈动画。我应该如何使用jQuery来实现这一点?
当前回答
我还发现了这样一个问题(挑战),在DB响应期间通知用户一些“信息”。
我的解决方案可能与这里的略有不同,它是好还是坏?我不知道,这对我来说已经足够了。
$.ajax({
type: 'ajax',
method: 'post',
url: '<?php echo base_url()?>skargi/osluga_skarg',
data: { full_date: full_date, head_title: head_title },
//async: false,
dataType: 'json',
beforeSend: function() { $body.addClass("loading"); },
success: function(data) {
$body.removeClass("loading");
其他回答
fontaweesome有一个加载动画图标,可以直接在你的项目中使用,没有任何额外的数据负担,如果你已经在使用字体awesome。
<span id="loading" style="display:none"><i class="fa fa-spinner fa-pulse"></i> PLEASE WAIT </span>
然后是jquery,你可以使用下面的代码来显示隐藏元素。
$(document).ajaxSend(function() {
$('#loading').show();
});
$(document).ajaxComplete(function() {
$('#loading').hide();
});
你可以用不同的方法来做。它可以是页面上的一个小状态,写着“正在加载…”,或者是在加载新数据时,整个元素使页面变灰。下面的方法将向您展示如何实现这两种方法。
设置
让我们从http://ajaxload.info开始一个漂亮的“加载”动画 我会用
让我们创建一个元素,我们可以在任何时候显示/隐藏ajax请求:
<div class="modal"><!-- Place at bottom of page --></div>
CSS
接下来,让我们给它一些天赋:
/* Start by setting display:none to make this hidden.
Then we position it in relation to the viewport window
with position:fixed. Width, height, top and left speak
for themselves. Background we set to 80% white with
our animation centered, and no-repeating */
.modal {
display: none;
position: fixed;
z-index: 1000;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: rgba( 255, 255, 255, .8 )
url('http://i.stack.imgur.com/FhHRx.gif')
50% 50%
no-repeat;
}
/* When the body has the loading class, we turn
the scrollbar off with overflow:hidden */
body.loading .modal {
overflow: hidden;
}
/* Anytime the body has the loading class, our
modal element will be visible */
body.loading .modal {
display: block;
}
最后是jQuery
好了,接下来是jQuery。下一部分其实很简单:
$body = $("body");
$(document).on({
ajaxStart: function() { $body.addClass("loading"); },
ajaxStop: function() { $body.removeClass("loading"); }
});
就是这样!当ajaxStart或ajaxStop事件被触发时,我们将一些事件附加到body元素。当ajax事件开始时,我们将“loading”类添加到主体中。当事件完成时,我们从主体中删除“loading”类。
看到它的行动:http://jsfiddle.net/VpDUG/4952/
根据https://www.w3schools.com/howto/howto_css_loader.asp,这是一个没有JS的2步过程:
1.在你想要旋转器的地方添加这个HTML: <div class="loader"></div>
2.添加下面的CSS来创建实际的旋转器:
.loader {
border: 16px solid #f3f3f3; /* Light grey */
border-top: 16px solid #3498db; /* Blue */
border-radius: 50%;
width: 120px;
height: 120px;
animation: spin 2s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
就像Mark H说的block kui就是方法。
Ex.:
<script type="text/javascript" src="javascript/jquery/jquery.blockUI.js"></script>
<script>
// unblock when ajax activity stops
$(document).ajaxStop($.unblockUI);
$("#downloadButton").click(function() {
$("#dialog").dialog({
width:"390px",
modal:true,
buttons: {
"OK, AGUARDO O E-MAIL!": function() {
$.blockUI({ message: '<img src="img/ajax-loader.gif" />' });
send();
}
}
});
});
function send() {
$.ajax({
url: "download-enviar.do",
type: "POST",
blablabla
});
}
</script>
奥林匹克广播服务公司。:我在http://www.ajaxload.info/上得到了ajax-loader.gif
至于实际加载的图像,请查看这个网站的一堆选项。
至于在请求开始时显示带有此图像的DIV,您有几个选择:
A)手动显示和隐藏图像:
$('#form').submit(function() {
$('#wait').show();
$.post('/whatever.php', function() {
$('#wait').hide();
});
return false;
});
B)使用ajaxStart和ajaxComplete:
$('#wait').ajaxStart(function() {
$(this).show();
}).ajaxComplete(function() {
$(this).hide();
});
使用这个元素将显示/隐藏任何请求。可能是好也可能是坏,取决于需要。
C)对特定的请求使用单独的回调:
$('#form').submit(function() {
$.ajax({
url: '/whatever.php',
beforeSend: function() { $('#wait').show(); },
complete: function() { $('#wait').hide(); }
});
return false;
});
推荐文章
- JSHint和jQuery: '$'没有定义
- 用jQuery检查Internet连接是否存在?
- 如何使用滑动(或显示)函数在一个表行?
- 如何用JavaScript截屏一个div ?
- 如何检测“搜索”HTML5输入的清除?
- 当元素从DOM中移除时触发事件
- Ajax成功事件不工作
- 引导下拉与Hover
- jQuery的.bind() vs. on()
- jQuery .live() vs .on()方法,用于在加载动态html后添加单击事件
- 事件。returnValue已弃用。请使用标准的event.preventDefault()
- 如何处理模式关闭事件在Twitter引导?
- 防止浏览器加载拖放文件
- 清除输入文本内的图标
- jQuery为ajax请求返回“parsererror”