Multiple containers in Lazy Load

て烟熏妆下的殇ゞ 提交于 2019-12-23 09:39:26

问题


I am trying to apply Lazy Load plugin to multiple containers. I found this similar question: Lazy Load on MULTIPLE horizontal containers.

this is my attempt: http://jsfiddle.net/BAFMC/

$(".p_outer_content").each(function() {
    var tthis = $(this);
    $(this).find('img').lazyload({
        container: tthis
    });
});​

But I have the same problem as the question mentioned, which is that Lazy load only applies to the last container (.p_outer_content) (which is the third one in the fiddle).

Does anyone know how to solve this or has other suggestion? Thanks in advance'

EDIT:

Ok, I tried to reapply the lazyload function each time one of the containers is scrolled:

$(".p_outer_content").each(function() {
    var tthis = $(this);
    $(this).find('img').lazyload({
        container: tthis
    });
});

$(".p_outer_content").scroll(function() {
    var tthis = $(this);
    $(this).find('img').lazyload({
        container: tthis
    });

});​

http://jsfiddle.net/BAFMC/4/

Which works, but I don't know if it a good way of solving it. Does anyone however come up with a better solution? Thanks'


回答1:


There is a bug in the LazyLoad plugin. When you provide a custom container, there was a global variable leak. I have added the minimum necessary fix in this fork here.

https://raw.github.com/marchingants/jquery_lazyload/master/jquery.lazyload.js

Here's a working demo http://jsfiddle.net/BAFMC/5/

I am using the github raw file directly in that example, but in your project, clone the file, minify it and use it locally.




回答2:


In order to get multiple divs to work with lazyload, you have to mention the container id's of the images explicitely. When you have two divs #container1 and #container2 then write:

$("#container1 img.lazy").lazyload({ container: $("#container1") });

$("#container2 img.lazy").lazyload({ container: $("#container2") });

Instead of: $("img.lazy").lazyload({ container: $("#container1") });

$("img.lazy").lazyload({ container: $("#container2") });



来源:https://stackoverflow.com/questions/11376016/multiple-containers-in-lazy-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!