admin管理员组文章数量:1327748
I've got a page that contains a list with about 100 images, and i dont want that the browser has to load all of it if the users only views approx. 10 of it.
So i've tried the jQuery.lazyload plugin on my page.
If i write:
$( function() {
$('.list li img').lazyload( { placeholder: 'n.gif' } );
});
for the code:
<ul class="list">
<li><img src="myawesomeimage.jpg"></li>
</ul>
The browser loads the images before the lazyload is applied to the images. I tried lazyload below the image tags without the document-ready construct - same results
I proxied the image-load through an php script that logs the access to the images - and it still loads everything at the beginning...
Anyone has an idea?
I've got a page that contains a list with about 100 images, and i dont want that the browser has to load all of it if the users only views approx. 10 of it.
So i've tried the jQuery.lazyload plugin on my page.
If i write:
$( function() {
$('.list li img').lazyload( { placeholder: 'n.gif' } );
});
for the code:
<ul class="list">
<li><img src="myawesomeimage.jpg"></li>
</ul>
The browser loads the images before the lazyload is applied to the images. I tried lazyload below the image tags without the document-ready construct - same results
I proxied the image-load through an php script that logs the access to the images - and it still loads everything at the beginning...
Anyone has an idea?
Share Improve this question asked Jan 20, 2010 at 13:40 BeerweasleBeerweasle 1,0091 gold badge13 silver badges16 bronze badges3 Answers
Reset to default 4Are you testing this locally or over an internet connection. The only thing I can think of is that if you're developing locally or on a local network, the images will load so fast that the plugin will not execute until after they've already loaded. One way to debug this might be to use the Firebug network tab. Add an $.ajax call to nowhere important before the execution of lazy load and then check out the network tab to see when the AJAX request goes out pared to when the images are loaded. If all is well you'll see the AJAX request before the image requests. If you don't see any image requests, make sure you're viewing the "All" group.
Also, make sure you have caching disabled when testing all of this because otherwise the browser will immediately load the images from cache.
Finally, I noticed that the plugin creator mentions that it doesn't work in IE when paired with jQuery 1.3. What browser are you testing in?
I have found different browsers have different behaviors. Some browsers will preload all the images even with lazyload in place.
If you can control the HTML generation, there is an absolute solution to this.
Output your image tags like so:
<img src=placeholder.png original=myimage.png>
In other words - make the src attribute point to some placeholder image (or omit it entirely), and point the original attribute point to the image you want lazy-loaded.
This will cause the browser to pre-load the placeholder image (best to use just 1 placeholder for all your img tags), and then wait to load the image pointed to by the original attribute. This works on all browsers in my experience.
Maybe it's worth it to check out a plugin called JAIL http://www.sebastianoarmelibattana./projects/jail which works well in the same scenario and has the advantage to work in all the browsers and to use data-* attribute. h You can read more about it at http://blog.sebarmeli./2011/01/04/reasons-why-i-wrote-the-plugin-jquery-asynchronous-image-loader-jail/ and you can find the source code on Github
本文标签: javascriptjQuery lazyload pluginStack Overflow
版权声明:本文标题:javascript - jQuery lazyload plugin - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742227898a2436669.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论