基本上我正在寻找的是这里的表现。
$.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来延迟加载到某些内容的图像。现在我可以看出,有和没有该代码的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);
加载时间仍然需要相同的时间......
感谢您的帮助!
答案 0 :(得分:11)
有没有办法把html放进去 页面,停止浏览器 加载那个html中的图像,和 然后在用户滚动时加载它们?
是的,是:jQuery航点: http://imakewebthings.github.com/jquery-waypoints/
http://jsfiddle.net/marcosfromero/BbA9M/
<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
属性不标准。
$('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');
}
})
答案 1 :(得分:0)
如果您已经知道要在页面上加载哪些图像,则可以在页面加载时为图像指定空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';
}