【问题标题】:Load html, then load images using Ajax in Jquery加载 html,然后在 Jquery 中使用 Ajax 加载图像
【发布时间】:2011-05-29 02:37:51
【问题描述】:

基本上我在这里寻找的是性能。

$.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" });
  });

我想使用 jquery 插件 lazy load 来延迟加载附加到某些内容的图像。现在我可以看出,有和没有该代码的延迟加载行的加载时间完全相同(它加载了大约 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);

加载时间仍然相同...

感谢您的帮助!

【问题讨论】:

    标签: jquery ajax performance lazy-loading


    【解决方案1】:

    有没有办法把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" />
    

    我在这段 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');
        }
    })
    

    【讨论】:

    • 不错!非常感谢您的快速回复。我想我可以通过使用图像正下方的隐藏字段来存储具有与图像相似的 id 的值来解决非标准问题。我会想办法的。再次感谢!
    【解决方案2】:

    如果你已经知道要在页面上加载哪些图片,你可以在页面加载时为图片赋予空的 src 属性,然后在页面加载事件中添加源。

    <img id='image1' src='' />
    

    空 src 属性

    window.onload = function () {
      ... the image path list ...
    var images = document.getElementsByTagName('img');
    
    // for loop then add the appropriate images in their correct place
    
    ... images[i].src = 'correct path';
    }
    

    【讨论】:

    • sigh - 不介意看到一个更有趣/解决问题的答案,而不是硬编码每个直接图像路径。
    • 使用隐藏字段来存储 src 的值,然后引用该隐藏字段。这是我最终做的,它是 xhtml 有效的。
    • 这是一个很好的选择。关键是能够随时加载图像
    猜你喜欢
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 2013-01-04
    相关资源
    最近更新 更多