Download html, then upload images using Ajax in jQuery

Basically what I'm looking for is performance here.

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

I want to use the jquery plugin, lazy loading , to lazy loading images that are added to some content. Now I can say that the loading time with the lazyload line of this code and without it is exactly the same (loading about 20 images). I assume the string is $ ("# content"). Append () waits for all images to load before uploading.

Is there a way to place html on a page, stop the browser from loading images in that html, and then load them as custom scrolls?

By the way, even when I do:

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

It takes as much time to download ...

Thanks for the help!

+3
2

html , html ?

, : jQuery waypoints: 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');
    }
})
+10

, , 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';
}
0

All Articles