性能差下面的代码上IE和Firefox工程确定,但铬讨厌它(它运行但确实是laggy)。我相信它可以使浏览器更友好,但是如何? itemPlaceholder是数百个100x100浮动div(例如图片占位符)。我使用的是jQuery 1.4.4和Chrome v10.0.648.127。jQuery的滚动事件 - 检测滚动到视图元件 - 在Chrome
$(function() {
ReplaceVisible();
$(this).scroll(function() {
ReplaceVisible();
});
});
function ReplaceVisible() {
$('.itemPlaceholder').each(function (index) {
if (HasBeenScrolledTo(this)) {
$itemPlaceholder = $(this);
$itemPlaceholder.replaceWith('<img src="bicycle.jpg" />');
}
else {
return false;
}
});
}
function HasBeenScrolledTo(elem) {
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elem).offset().top;
return elemTop < docViewBottom;
}
编辑:与replaceWith
否则我们得到很多附加至相同的元素图像的替换append
。
而直接从马的嘴:http://ejohn.org/blog/learning-from-twitter/ – 2011-03-11 04:59:40
嗯,Chrome并产生更多的滚动事件,但magnatude它不是订单更多。这个问题似乎是由编辑DOM(append)引起的。删除该行,并滚动平稳。 – Sprintstar 2011-03-11 09:18:52