加载html,然后在Jquery中使用Ajax加载图像

Dav*_*ret 4 ajax performance jquery lazy-loading

基本上我正在寻找的是这里的表现.

$.post(link,     
  function(data){
         //removes the image that was displayed while loading
         $("#loadingImg").remove();

         //append the new content, into the content div
         //and add a div around the new content in order to lazy load
         $("#content").append('<div id="new-page">' + data + '</div>');
         //lazy load the images inside the new div
         $("#new-page").find("img").lazyload({ 
                 placeholder : "/images/white.png", effect : "fadeIn" });
  });
Run Code Online (Sandbox Code Playgroud)

我想使用jquery插件,延迟加载,延迟加载到某些内容的图像.现在我可以看出,有和没有该代码的lazyload行的加载时间完全相同(它加载了大约20个图像).我现在假设$("#content").append()行在追加之前等待加载所有图像.

有没有办法把html放在页面中,停止浏览器加载该html中的图像,然后在用户滚动时加载它们?

顺便说一句,即使我这样做:

data = '<div id="new-page-' + pageCounter + '">' + data + '</div>';
var newData = $(data);
newData.find("img").lazyload({ placeholder : "/images/white.png", effect : "fadeIn" });
$("#content").append(newData);
Run Code Online (Sandbox Code Playgroud)

它仍然需要相同的时间来加载......

谢谢您的帮助!

mar*_*ero 11

有没有办法把html放在页面中,停止浏览器加载该html中的图像,然后在用户滚动时加载它们?

是的,是:jQuery航点:http: //imakewebthings.github.com/jquery-waypoints/

工作范例:

http://jsfiddle.net/marcosfromero/BbA9M/

HTML:

<img id="first" src="" width="364" height="126" deferred="http://www.google.com/images/logos/ps_logo2.png" />

<div style="height: 500px; ">Some vertical space. Scroll to the bottom</div>

<img id="second" src="" width="364" height="126" deferred="http://www.google.com/images/logos/ps_logo2.png" />
Run Code Online (Sandbox Code Playgroud)

我在这个HTML代码中找到的唯一缺点是该deferred属性是非标准的.

jQuery的:

$('img').waypoint(function(event, direction) {
    // set the image src attribute from the deferred one, which has the real URL
    this.src = this.getAttribute('deferred');
}, {
    triggerOnce: true, // load the image just once
    offset: function() {
        // calculate where the event should be fired
        // in this case, when the whole image is visible
        return $.waypoints('viewportHeight') - $(this).height();
        // if you want to load the image when the top of it is visible change it for
        // return $.waypoints('viewportHeight');
    }
})
Run Code Online (Sandbox Code Playgroud)