【发布时间】:2010-01-28 21:55:12
【问题描述】:
我有 4 个DIV,我希望在您滚动其中一个 div 时触发一个 scroll 事件。这是下面的代码。
$('#div1, #div2, #div3, #div4').scroll(function() {
alert('...');
});
在 Firefox/Chrome 中,它运行得很快;但是,在 Internet Explorer 中,它运行得非常慢,以至于它实际上阻止了我滚动 div。
我使用的是最新版本的 JQuery (v.1.4.1)。
问题:有没有更有效的方式来运行上面的代码?如果有,怎么做?
更新:既然有人问了,我已经在我的整个代码下面包含了:
$('#div1, #div2, #div3, #div4').scroll(function() {
/* find the closest (hlisting) home listing to the middle of the scrollwindow */
var scrollElemPos = activeHomeDiv.offset();
var newHighlightDiv = $(document.elementFromPoint(
scrollElemPos.left + activeHomeDiv.width() / 2,
scrollElemPos.top + activeHomeDiv.height() / 2)
).closest('.hlisting');
if(newHighlightDiv.is(".HighlightRow")) return;
$('.HighlightRow').removeClass("HighlightRow");
newHighlightDiv.addClass('HighlightRow');
/* change the map marker icon to denote the currently focused on home */
var activeHomeID = newHighlightDiv.attr("id");
if (activeHomeMarkerID) {
// unset the old active house marker to a normal icon
map.markers[activeHomeMarkerID].setIcon('http://example.com/images/house-icon.png');
}
activeHomeMarkerID = activeHomeID.substring(4); // set the new active marker id
map.markers[activeHomeMarkerID].setIcon('http://example.com/images/house-icon-active.png');
});
更新 2:
所以我在下面和 IE 中实现了计时器选项,它仍然很慢。还有其他想法吗?
【问题讨论】:
-
我可以看到该代码没有任何问题。尝试仅使用这 4 个 div、jquery.js 和您的滚动功能创建一个空白页面,以查看页面中是否有其他内容导致速度变慢。滚动时不要 alert()。
-
你真的每次滚动都在提醒吗?
-
@petersendidit,不 - 这只是填充代码
标签: jquery javascript micro-optimization