请解释一下这段代码

发布于 2024-11-13 09:27:18 字数 1402 浏览 4 评论 0原文

尝试理解下面的代码,因为我想对其进行一些自定义。 根据注释,回调函数将最后一个图像作为参数传递给加载。有人可以解释一下回调是如何仅传递到最后一张图像的吗(我理解为什么,我只是不知道如何)?从我阅读代码的方式来看,它似乎已传递给所有人。

干杯。

// $('img.photo',this).imagesLoaded(myFunction)
// execute a callback when all images have loaded.
// needed because .load() doesn't work on cached images

// mit license. paul irish. 2010.
// webkit fix from Oren Solomianik. thx!

// callback function is passed the last image to load
//   as an argument, and the collection as 'this'


$.fn.imagesLoaded = function(callback){
  var elems = this.filter('img'),
      len   = elems.length,
      blank = 'data:image/gif;base64,' +
          'R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';

  elems.bind('load',function(){
      if (--len <= 0 && this.src !== blank){ callback.call(elems,this); }
  }).each(function(){
     // cached images don't fire load sometimes, so we reset src.
     if (this.complete || this.complete === undefined){
        var src = this.src;
        // webkit hack from
        // groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
        // data uri bypasses webkit log warning (thx doug jones)
        this.src = blank;
        this.src = src;
     }  
  }); 

  return this;
};

Trying to make sense of the code below, as I want to customise it a bit.
As per the comments, the callback function is passed the last image to load as an argument. Can someone please explain how the callback is passed only to the last image (I understand why, I just don't know how)? From the way I'm reading the code it seems to be passed to all of them.

Cheers.

// $('img.photo',this).imagesLoaded(myFunction)
// execute a callback when all images have loaded.
// needed because .load() doesn't work on cached images

// mit license. paul irish. 2010.
// webkit fix from Oren Solomianik. thx!

// callback function is passed the last image to load
//   as an argument, and the collection as 'this'


$.fn.imagesLoaded = function(callback){
  var elems = this.filter('img'),
      len   = elems.length,
      blank = 'data:image/gif;base64,' +
          'R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';

  elems.bind('load',function(){
      if (--len <= 0 && this.src !== blank){ callback.call(elems,this); }
  }).each(function(){
     // cached images don't fire load sometimes, so we reset src.
     if (this.complete || this.complete === undefined){
        var src = this.src;
        // webkit hack from
        // groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
        // data uri bypasses webkit log warning (thx doug jones)
        this.src = blank;
        this.src = src;
     }  
  }); 

  return this;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回眸一遍 2024-11-20 09:27:18

仅当 len(递减)小于或等于 0 时才会调用回调函数。由于它以元素长度的值(或有多少个元素)开始,并且在加载每个图像时递减,因此只有最后一个图像将使表达式为真。

您可以将这部分代码理解

   elems.bind('load',function(){
      if (--len <= 0 && this.src !== blank){ callback.call(elems,this); }
  })

为“将此函数绑定到 elems 中每个元素的加载方法”。因此,每个元素在加载时都会调用该函数,因此调用了 function () {...}。第一个图像递减 len,然后检查它是否小于或等于 0,然后下一个元素在加载完成时执行相同的操作,依此类推,直到最后一个图像递减 len,然后检查它是否小于 0。它是最后一个图像,它返回 true,如果 src 不为空,它将运行callback.call()。

The callback function is only called if len (decremented) is less than or equal to zero. Since it starts off with the value of the length of the elements (or how many there are), and is decremented when each image is loaded, only the last image will make the expression true.

You can sort of read this part of the code:

   elems.bind('load',function(){
      if (--len <= 0 && this.src !== blank){ callback.call(elems,this); }
  })

as "bind this function to the load method for each element in elems." So, each element calls the function when it is loaded, so the function () {...} is called. The first image decrements len and then checks if it is less than or equal to 0, then the next element does the same when its done loading, and so on until the last image decrements len and then checks if it is less than 0. Since it's the last image, it returns true and if the src is not blank, it runs callback.call().

昵称有卵用 2024-11-20 09:27:18

下面的行就可以解决这个问题

if (--len <= 0 && this.src !== blank){ callback.call(elems,this); }

基本上,load 事件附加到每个 img 标签上,如果加载被触发,则 len 变量将递减,如果 len len < ;= 0 回调将被执行。 load 事件本身将由替换图像的 each() 函数触发。

The following line does the trick

if (--len <= 0 && this.src !== blank){ callback.call(elems,this); }

Basically, the load event is attached to each img tag, and if load is fired the len variable will be decremented and if len <= 0 the callback will be executed. The load event itself will be triggered by the each() function that replaces the images.

夏末 2024-11-20 09:27:18

对最后一个元素触发的评估以及对回调函数的调用结果可以在此处找到。

if (--len <= 0 && this.src !== blank){ callback.call(elems,this); }

请注意,它绑定到 elems 中的每个项目,以在 load 事件上触发 - 执行此操作的代码是:

elems.bind('load',function(){ //.. Basically binds the following function to every item in elemns on load.

所以基本上,这意味着当加载图像时,它将递减 len 变量 (--len) 并检查它是否小于或等于零,并确保该元素具有源代码;如果这两个条件都满足,这几乎意味着它是集合中触发加载事件的最后一个元素,那么回调函数将触发。

The evaluation for firing on the last element and resulting call to the callback func is found here

if (--len <= 0 && this.src !== blank){ callback.call(elems,this); }

Note that it is bound to each item in elems, to fire off on the load event -- the code to do this is:

elems.bind('load',function(){ //.. Basically binds the following function to every item in elemns on load.

So basically, this means that when an image loads, it will de-increment the len variable (--len) and check if that's less than or equal to zero, and make sure this element has an src; if both of those conditions are satisfied, which pretty much has to mean it is the last element in the set to fire the load event, then the callback func will fire.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文