执行$ .ajax时显示加载图像

Upv*_*ote 108 javascript css jquery

我只是想知道如何显示一个图像,表明异步请求正在运行.我使用以下代码执行异步请求:

$.ajax({
  url: uri,
  cache: false,
  success: function(html){
    $('.info').append(html);
  }
});
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Zac*_*oom 241

当然,您可以在发出请求之前显示它,并在完成后隐藏它:

$('#loading-image').show();
$.ajax({
      url: uri,
      cache: false,
      success: function(html){
        $('.info').append(html);
      },
      complete: function(){
        $('#loading-image').hide();
      }
    });
Run Code Online (Sandbox Code Playgroud)

我通常更喜欢将它绑定到全局ajaxStart和ajaxStop事件的更通用的解决方案,这样它就会显示所有ajax事件:

$('#loading-image').bind('ajaxStart', function(){
    $(this).show();
}).bind('ajaxStop', function(){
    $(this).hide();
});
Run Code Online (Sandbox Code Playgroud)

  • 从jQuery 1.9开始,AJAX事件应仅附加到`document`.见http://stackoverflow.com/questions/2275342/jquery-ajaxstart-doesnt-get-triggered (29认同)

jEr*_*myB 54

使用ajax对象的beforeSend和complete函数.最好从beforeSend内部显示gif,以便将所有行为封装在单个对象中.使用success函数隐藏gif时要小心.如果请求失败,您可能仍希望隐藏gif.为此,请使用"完成"功能.它看起来像这样:

$.ajax({
    url: uri,
    cache: false,
    beforeSend: function(){
        $('#image').show();
    },
    complete: function(){
        $('#image').hide();
    },
    success: function(html){
       $('.info').append(html);
    }
});
Run Code Online (Sandbox Code Playgroud)


小智 18

HTML代码:

<div class="ajax-loader">
  <img src="{{ url('guest/images/ajax-loader.gif') }}" class="img-responsive" />
</div>
Run Code Online (Sandbox Code Playgroud)

CSS代码:

.ajax-loader {
  visibility: hidden;
  background-color: rgba(255,255,255,0.7);
  position: absolute;
  z-index: +100 !important;
  width: 100%;
  height:100%;
}

.ajax-loader img {
  position: relative;
  top:50%;
  left:50%;
}
Run Code Online (Sandbox Code Playgroud)

JQUERY代码:

$.ajax({
  type:'POST',
  beforeSend: function(){
    $('.ajax-loader').css("visibility", "visible");
  },
  url:'/quantityPlus',
  data: {
   'productId':p1,
   'quantity':p2,
   'productPrice':p3},
   success:function(data){
     $('#'+p1+'value').text(data.newProductQuantity);
     $('#'+p1+'amount').text("? "+data.productAmount);
     $('#totalUnits').text(data.newNoOfUnits);
     $('#totalAmount').text("? "+data.newTotalAmount);
  },
  complete: function(){
    $('.ajax-loader').css("visibility", "hidden");
  }
});

}
Run Code Online (Sandbox Code Playgroud)


The*_*ild 11

我认为如果你有大量的 $.ajax 调用,这可能会更好

$(document).ajaxSend(function(){
    $(AnyElementYouWantToShowOnAjaxSend).fadeIn(250);
});
$(document).ajaxComplete(function(){
    $(AnyElementYouWantToShowOnAjaxSend).fadeOut(250);
});
Run Code Online (Sandbox Code Playgroud)

笔记:

如果你使用 CSS。当 ajax 从后端代码中获取数据时,您想要显示的元素必须是这样的。

AnyElementYouWantToShowOnAjaxSend {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh; /* to make it responsive */
    width: 100vw; /* to make it responsive */
    overflow: hidden; /*to remove scrollbars */
    z-index: 99999; /*to make it appear on topmost part of the page */
    display: none; /*to make it visible only on fadeIn() function */
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案,因为它是通用的! (2认同)

jEr*_*myB 7

人们通常在ajax调用期间显示的"图像"是GIF动画.由于无法确定ajax请求的完成百分比,因此使用的GIF动画是不确定的微调器.这只是一个反复重复的图像,就像一个不同大小的圆球.一个很好的网站来创建自己的自定义不确定微调器是http://ajaxload.info/