简体   繁体   English

Jquery中的预加载器

[英]Preloader in Jquery

I've coded a fancy preloader with jQuery for my project.我已经为我的项目用 jQuery 编写了一个漂亮的预加载器。 While my AJAX query is processed it is activated.当我的 AJAX 查询被处理时,它被激活。

I have used to load it:我曾经加载它:

$(document).ajaxStart(function) () {
    #preloder is displayed 
} 

And to stop it:并阻止它:

$(document).ajaxStart(function) () {
    #preloder is turned off 
} 

Problem is my AJAX returns images src that are then diplayed in a <div> but some of the pictures are really heavy and most of the time my preloader stops before my pictures are completely loaded.问题是我的 AJAX 返回图像src ,然后在<div>显示,但有些图片真的很重,而且大部分时间我的预加载器在我的图片完全加载之前停止。

I went through jquery documentation and found a .load(function).我浏览了 jquery 文档并找到了一个 .load(function)。 But it seems that it has been removed from jQuery.但似乎它已从 jQuery 中删除。 It seems that it has been replaced by .trigger("load") .似乎它已被.trigger("load")取代。 Can it be useful for my problem?它对我的问题有用吗? I tried to implement it but with no sucess.我试图实施它,但没有成功。

Quick answer from @Aureliano Far Suau that worked beautifully for me. @Aureliano Far Suau 的快速回答对我来说效果很好。 No plugin needed:无需插件:

$('img').on('load', function() {
          // do something
    });

For multiple images:对于多个图像:

var loaded = 0;
$('img').on('load', function() {
   loaded++;
   if(loaded == $('img').length){
      // do something
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM