我认为你的问题是scrollTo http://flesler.blogspot.com/2007/10/jqueryscrollto.html uses animate http://api.jquery.com/animate/:
// From the plugin's source
function animate( callback ){
$elem.animate( attr, duration, settings.easing, callback && function(){
callback.call(this, target, settings);
});
};
And animate
使用计时器来执行动画。结果是.scrollTo
将在滚动完成之前返回,并且您将重新绑定滚动处理程序scrollTo
仍在滚动。因此,就会发生你意想不到的事件。
一个简单的解决方案是使用一个标志来告诉makeVisibleWhatMust
that scrollTo
正在滚动并使用scrollTo
完成后清除标志的回调,如下所示:
function makeVisibleWhatMust() {
// Ignore the event if we're doing the scrolling.
if(makeVisibleWhatMust.isScrolling)
return;
$('#testContainer').text( $('#testContainer').text() + 'called\n');
$('table.scrollTable').each(function() {
var table = this;
$($('tr.alwaysVisible', table).get().reverse()).each(function() {
$(this).insertAfter( $('tr:in-viewport:not(.alwaysVisible)', table)[0] );
});
makeVisibleWhatMust.isScrolling = true;
$(window).scrollTo($('tr.alwaysVisible')[0], {
onAfter: function() { makeVisibleWhatMust.isScrolling = false; }
});
}
);
}
makeVisibleWhatMust.isScrolling = false;
这是一个似乎有效的实时版本:http://jsfiddle.net/ambigously/ZEx6M/1/ http://jsfiddle.net/ambiguous/ZEx6M/1/