如何在jQuery中显示加载微调器?
在Prototype中,我可以用以下代码显示“加载…”图像:如何在jQuery中显示加载微调器?,jquery,spinner,prototypejs,equivalence,language-interoperability,Jquery,Spinner,Prototypejs,Equivalence,Language Interoperability,在Prototype中,我可以用以下代码显示“加载…”图像: var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onLoading: showLoad, onComplete: showResponse} ); function showLoad () { ... } 在jQuery中,我可以使用以下命令将服务器页面加载到元素中: $('#message').load('index.php?p
var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars,
onLoading: showLoad, onComplete: showResponse} );
function showLoad () {
...
}
在jQuery中,我可以使用以下命令将服务器页面加载到元素中:
$('#message').load('index.php?pg=ajaxFlashcard');
但是,如何像在Prototype中那样将加载微调器附加到此命令?有几种方法。我的首选方法是将函数附加到元素本身上的ajaxStart/Stop事件
$('#loadingDiv')
.hide() // Hide it initially
.ajaxStart(function() {
$(this).show();
})
.ajaxStop(function() {
$(this).hide();
})
;
每当您进行任何Ajax调用时,ajaxStart/Stop函数都将启动
更新:从jQuery 1.8开始,文档说明.ajaxStart/Stop
只应附加到文档
。这会将上述代码段转换为:
var $loading = $('#loadingDiv').hide();
$(document)
.ajaxStart(function () {
$loading.show();
})
.ajaxStop(function () {
$loading.hide();
});
您可以在AJAX调用之前将动画图像插入DOM,并执行内联函数将其删除
$("#myDiv").html('<img src="images/spinner.gif" alt="Wait" />');
$('#message').load('index.php?pg=ajaxFlashcard', null, function() {
$("#myDiv").html('');
});
$(“#myDiv”).html(“”);
$('#message').load('index.php?pg=ajaxFlashcard',null,function(){
$(“#myDiv”).html(“”);
});
这将确保您的动画在后续请求中从同一帧开始(如果这很重要的话)。请注意,旧版本的IE可能在动画方面有困难
祝你好运 JavaScript
使用加载插件:
对于jQuery,我使用
jQuery.ajaxSetup({
beforeSend: function() {
$('#loader').show();
},
complete: function(){
$('#loader').hide();
},
success: function() {}
});
变体:我在主页的左上角有一个id=“logo”的图标;当ajax工作时,一个微调器gif会覆盖在顶部(透明)
jQuery.ajaxSetup({
beforeSend: function() {
$('#logo').css('background', 'url(images/ajax-loader.gif) no-repeat')
},
complete: function(){
$('#logo').css('background', 'none')
},
success: function() {}
});
您只需将加载程序映像分配给稍后将使用Ajax调用加载内容的同一标记:
$("#message").html('<span>Loading...</span>');
$('#message').load('index.php?pg=ajaxFlashcard');
$(“#message”).html('Loading…');
$('#message').load('index.php?pg=ajaxFlashcard');
您也可以用图像标记替换span标记。我这样做:
var preloaderdiv = '<div class="thumbs_preloader">Loading...</div>';
$('#detail_thumbnails').html(preloaderdiv);
$.ajax({
async:true,
url:'./Ajaxification/getRandomUser?top='+ $(sender).css('top') +'&lef='+ $(sender).css('left'),
success:function(data){
$('#detail_thumbnails').html(data);
}
});
var preforderdiv='Loading…';
$('detail#u缩略图').html(preforderdiv);
$.ajax({
async:true,
url:'./Ajaxification/getRandomUser?top='+$(发送方).css('top')+'&lef='+$(发送方).css('left'),
成功:功能(数据){
$('detail#u缩略图').html(数据);
}
});
我在jQuery UI对话框中使用了以下内容。(也许它可以与其他ajax回调一起使用?)
$('').load('/ajax.html')。对话框({
身高:300,
宽度:600,
标题:“等等…”
});
包含动画加载gif,直到ajax调用完成时其内容被替换。我认为您是对的。
这个方法太全局了
但是,当AJAX调用对页面本身没有影响时,这是一个很好的默认设置。(例如,后台保存)。(对于某个ajax调用,您可以通过传递“global”:false来关闭它-请参阅
当AJAX调用要刷新页面的一部分时,我希望我的“加载”图像特定于刷新的部分
想象一下,如果你能简单地写下如下内容,那该有多酷:
$("#component_to_refresh").ajax( { ... } );
这将显示该部分的“加载”。
下面是我编写的一个函数,它也处理“加载”显示,但它特定于您在ajax中刷新的区域
首先,让我告诉你如何使用它
<!-- assume you have this HTML and you would like to refresh
it / load the content with ajax -->
<span id="email" name="name" class="ajax-loading">
</span>
<!-- then you have the following javascript -->
$(document).ready(function(){
$("#email").ajax({'url':"/my/url", load:true, global:false});
})
$(文档).ready(函数(){
$(“#email”).ajax({'url':“/my/url”,load:true,global:false});
})
这就是功能——一个基本的开始,你可以根据自己的意愿进行增强。它非常灵活
jQuery.fn.ajax = function(options)
{
var $this = $(this);
debugger;
function invokeFunc(func, arguments)
{
if ( typeof(func) == "function")
{
func( arguments ) ;
}
}
function _think( obj, think )
{
if ( think )
{
obj.html('<div class="loading" style="background: url(/public/images/loading_1.gif) no-repeat; display:inline-block; width:70px; height:30px; padding-left:25px;"> Loading ... </div>');
}
else
{
obj.find(".loading").hide();
}
}
function makeMeThink( think )
{
if ( $this.is(".ajax-loading") )
{
_think($this,think);
}
else
{
_think($this, think);
}
}
options = $.extend({}, options); // make options not null - ridiculous, but still.
// read more about ajax events
var newoptions = $.extend({
beforeSend: function()
{
invokeFunc(options.beforeSend, null);
makeMeThink(true);
},
complete: function()
{
invokeFunc(options.complete);
makeMeThink(false);
},
success:function(result)
{
invokeFunc(options.success);
if ( options.load )
{
$this.html(result);
}
}
}, options);
$.ajax(newoptions);
};
jQuery.fn.ajax=函数(选项)
{
var$this=$(this);
调试器;
函数invokeFunc(func,参数)
{
if(类型(函数)=“函数”)
{
func(参数);
}
}
功能(obj,think)
{
如果(思考)
{
html('Loading…');
}
其他的
{
obj.find(“.loading”).hide();
}
}
函数makeMeThink(思考)
{
if($this.is(“.ajax加载”))
{
_想想(这个,想想);
}
其他的
{
_想想(这个,想想);
}
}
options=$.extend({},options);//使选项不为null-荒谬,但仍然有效。
//了解更多关于ajax事件的信息
var newoptions=$.extend({
beforeSend:function()
{
invokeFunc(options.beforeSend,null);
makeMeThink(true);
},
完成:函数()
{
invokeFunc(options.complete);
makeMeThink(假);
},
成功:功能(结果)
{
invokeFunc(options.success);
if(options.load)
{
$this.html(结果);
}
}
},选项);
$.ajax(新选项);
};
您可以使用jQuery的.ajax
函数,在发送之前使用它的选项,
并定义一些函数,在这些函数中可以显示loader div之类的内容,在成功选项中可以隐藏该loader div
jQuery.ajax({
type: "POST",
url: 'YOU_URL_TO_WHICH_DATA_SEND',
data:'YOUR_DATA_TO_SEND',
beforeSend: function() {
$("#loaderDiv").show();
},
success: function(data) {
$("#loaderDiv").hide();
}
});
你可以拥有任何旋转的Gif图像。根据你的配色方案,这是一个很棒的AJAX加载生成器网站:除了为AJAX事件设置全局默认值外,你还可以为特定元素设置行为。也许仅仅更改它们的类就足够了
$('#myForm').ajaxSend( function() {
$(this).addClass('loading');
});
$('#myForm').ajaxComplete( function(){
$(this).removeClass('loading');
});
CSS示例,使用微调器隐藏#myForm:
.loading {
display: block;
background: url(spinner.gif) no-repeat center middle;
width: 124px;
height: 124px;
margin: 0 auto;
}
/* Hide all the children of the 'loading' element */
.loading * {
display: none;
}
最后,我对文件进行了两次更改
文档
。这使得只过滤一些ajax请求变得更加困难。所以$(document)
.hide() // hide it initially
.ajaxSend(function(event, jqxhr, settings) {
if (settings.url !== "ajax/request.php") return;
$(".spinner").show();
})
.ajaxComplete(function(event, jqxhr, settings) {
if (settings.url !== "ajax/request.php") return;
$(".spinner").hide();
})
如果您不想编写自己的代码,还有很多插件可以做到这一点:
$.ajax()
,您可以使用如下内容:
$.ajax({
url: "destination url",
success: sdialog,
error: edialog,
// shows the loader element before sending.
beforeSend: function() {
$("#imgSpinner1").show();
},
// hides the loader after completion of request, whether successfull or failor.
complete: function() {
$("#imgSpinner1").hide();
},
type: 'POST',
dataType: 'json'
});
<style>
#ajaxSpinnerImage {
display: none;
}
</style>
<div id="ajaxSpinnerContainer">
<img src="~/Content/ajax-loader.gif" id="ajaxSpinnerImage" title="working..." />
</div>
<script>
$(document).ready(function () {
$(document)
.ajaxStart(function () {
$("#ajaxSpinnerImage").show();
})
.ajaxStop(function () {
$("#ajaxSpinnerImage").hide();
});
var owmAPI = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=YourAppID";
$.getJSON(owmAPI)
.done(function (data) {
alert(data.coord.lon);
})
.fail(function () {
alert('error');
});
});
</script>
尽管该设置名为“beforeSend”,但从即日起,如果键入:“GET”
,则将调用.show()
函数
.loading {
display: block;
background: url(spinner.gif) no-repeat center middle;
width: 124px;
height: 124px;
margin: 0 auto;
}
/* Hide all the children of the 'loading' element */
.loading * {
display: none;
}
$(document)
.hide() // hide it initially
.ajaxSend(function(event, jqxhr, settings) {
if (settings.url !== "ajax/request.php") return;
$(".spinner").show();
})
.ajaxComplete(function(event, jqxhr, settings) {
if (settings.url !== "ajax/request.php") return;
$(".spinner").hide();
})
$.ajax({
url: "destination url",
success: sdialog,
error: edialog,
// shows the loader element before sending.
beforeSend: function() {
$("#imgSpinner1").show();
},
// hides the loader after completion of request, whether successfull or failor.
complete: function() {
$("#imgSpinner1").hide();
},
type: 'POST',
dataType: 'json'
});
$(document).ajaxStart(function() {
$(".loading").show();
});
$(document).ajaxStop(function() {
$(".loading").hide();
});
$(document).ajaxStart ->
$(".loading").show()
$(document).ajaxStop ->
$(".loading").hide()
<style>
#ajaxSpinnerImage {
display: none;
}
</style>
<div id="ajaxSpinnerContainer">
<img src="~/Content/ajax-loader.gif" id="ajaxSpinnerImage" title="working..." />
</div>
<script>
$(document).ready(function () {
$(document)
.ajaxStart(function () {
$("#ajaxSpinnerImage").show();
})
.ajaxStop(function () {
$("#ajaxSpinnerImage").hide();
});
var owmAPI = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=YourAppID";
$.getJSON(owmAPI)
.done(function (data) {
alert(data.coord.lon);
})
.fail(function () {
alert('error');
});
});
</script>
$.ajax({
url: requestUrl,
data: data,
dataType: 'JSON',
processData: false,
type: requestMethod,
async: true, <<<<<<------ set async to true
accepts: 'application/json',
contentType: 'application/json',
success: function (restResponse) {
// something here
},
error: function (restResponse) {
// something here
}
});
jTarget.ajaxloader(); // (re)start the loader
$.post('/libs/jajaxloader/demo/service/service.php', function (content) {
jTarget.append(content); // or do something with the content
})
.always(function () {
jTarget.ajaxloader("stop");
});
$('#loading-image').html('<img src="/images/ajax-loader.gif"> Sending...');
$.ajax({
url: uri,
cache: false,
success: function(){
$('#loading-image').html('');
},
error: function(jqXHR, textStatus, errorThrown) {
var text = "Error has occured when submitting the job: "+jqXHR.status+ " Contact IT dept";
$('#loading-image').html('<span style="color:red">'+text +' </span>');
}
});
$.ajax({
url: "@Url.Action("MyJsonAction", "Home")",
type: "POST",
dataType: "json",
data: {parameter:variable},
//async: false,
error: function () {
},
success: function (data) {
if (Object.keys(data).length > 0) {
//use data
}
$('#ajaxspinner').hide();
}
});
$("#MyDropDownID").change(function () {
$('#ajaxspinner').show();