I had a similar problem and here's how I solved it:
In a nut shell:
When creating the element I made src='Blank.gif'
and I created setAttribute("data-src",response[i].photo_url)
. Seeing as Blank.gif
doesn't exist, it would be rendered as a broken image link if the below didn't work, or it cant load fast enough...
Then, onscoll i checked to see when it was in view and set the src to the 'data-src'. This works very well for me...
The details!
window.onscroll = function() {
var top = document.body.scrollTop;
var bottom = document.documentElement.clientHeight + top;
var img;
for (var i=startImage;i<index;i++){
img = document.getElementById("image" + i);
var a = img.style.top.substring(0,img.style.top.length-2);
var b = bottom;//Just for simplicity...
if (a < b) {
if (img.src.indexOf('Blank.gif') != -1) {
img.src = img.getAttribute("data-src");
}
startImage++;
}
}
}
Hope this can save someone else 2 1/2 hours of searching/work!
Feel free to let me know if you think there's a way to make the above more efficient!