请解释一下这段代码
尝试理解下面的代码,因为我想对其进行一些自定义。 根据注释,回调函数将最后一个图像作为参数传递给加载
。有人可以解释一下回调是如何仅传递到最后一张图像的吗(我理解为什么,我只是不知道如何)?从我阅读代码的方式来看,它似乎已传递给所有人。
干杯。
// $('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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当 len(递减)小于或等于 0 时才会调用回调函数。由于它以元素长度的值(或有多少个元素)开始,并且在加载每个图像时递减,因此只有最后一个图像将使表达式为真。
您可以将这部分代码理解
为“将此函数绑定到 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:
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().
下面的行就可以解决这个问题
基本上,load 事件附加到每个
img
标签上,如果加载被触发,则len
变量将递减,如果len
len < ;= 0
回调将被执行。load
事件本身将由替换图像的each()
函数触发。The following line does the trick
Basically, the load event is attached to each
img
tag, and if load is fired thelen
variable will be decremented and iflen <= 0
the callback will be executed. Theload
event itself will be triggered by theeach()
function that replaces the images.对最后一个元素触发的评估以及对回调函数的调用结果可以在此处找到。
请注意,它绑定到 elems 中的每个项目,以在
load
事件上触发 - 执行此操作的代码是:所以基本上,这意味着当加载图像时,它将递减 len 变量 (
--len
) 并检查它是否小于或等于零,并确保该元素具有源代码;如果这两个条件都满足,这几乎意味着它是集合中触发加载事件的最后一个元素,那么回调函数将触发。The evaluation for firing on the last element and resulting call to the callback func is found here
Note that it is bound to each item in elems, to fire off on the
load
event -- the code to do this is: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.