简体   繁体   English

滚动时如何优化网络/移动网络上的图像加载

[英]How to optimize loading of images on web/mobile web when scrolling

I am now loading all the images via my template when the image loads 现在,当图像加载时,我正在通过模板加载所有图像

 //For example, I will just pass the image url into the tempate when the page renders
  %div{class: "img_container <%= answer.image_scale %> <%=answer.show_image%>"}
    <% if (media_con.standard_resolution) { %>
    %a.fancy{href: "<%= media_con.standard_resolution %>"}
      %img{src: "<%=media_con.standard_resolution%>"}

I realize that by doing this (esp if I am loading many images on the page), it causes inefficieny esp apparent on the mobile web. 我意识到这样做(如果我要在页面上加载很多图像,尤其是在网页上)会导致效率低下,尤其是在移动网络上。

How can I load images only when they are in view (scrolling), maybe just load a thumbnail image and load the actual image when the view with the image comes to view. 如何仅在视图中加载图像(滚动),或者在视图和图像一起显示时才加载缩略图图像和实际图像。

Any advice here is appreciated 任何建议在这里表示赞赏

There are a number of libraries to achieve this. 有许多库可以实现此目的。 I've personally used the Lazy Load Plugin for jQuery ( demo ). 我亲自使用了jQuery的惰性加载插件演示 )。 Instead of setting <img src> property in the template, you should set a property called data-original to the url of the image: 而不是在模板中设置<img src>属性,应将一个名为data-original的属性设置为图像的URL:

%img{data-original: "<%=media_con.standard_resolution%>", src="placeholder.png"}

And on the client side code (perhaps in Backbone.view.render ) you initialize the library: 然后在客户端代码(可能在Backbone.view.render )中初始化库:

$("img[data-original]").lazyload();

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

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