在不改变文档的高度(即在底部添加额外填充)的情况下,您将始终存在此问题。
创建一个演示来说明概念:http://jsfiddle.net/mrchief/PYsyN/9/
CSS
<style>
.current { font-weight: bold; }
</style>
JS
function smoothScroll(elemId) {
// remove existing highlights
$('.current').css({backgroundColor: "transparent"}).removeClass('current');
var top = $(elemId).offset().top;
// do a smooth scroll
$('html, body').animate({scrollTop:top}, 500, function(){
// add an highlight
$(elemId).animate({backgroundColor: "#68BFEF" }, 500, function() {
// keep tab of current so that style can be reset later
$(elemId).addClass('current');
});
});
}
// when landing directly
if (document.location.hash) {
smoothScroll(document.location.hash);
}
$('a[href*=#]').click(function() {
// utilizing the fact that named acnhor has a corresponding id element
var elemId = $(this).attr('href');
smoothScroll(elemId);
});
如果不控制服务器(例如,在第二个例子中)我真的不知道如何改变这种行为。图片/额外'
'可能是您可以控制的网页上的最佳解决方案。 –