【发布时间】:2013-12-22 05:39:50
【问题描述】:
我正在尝试在使用锚点时调整滚动的位置,因为我使用的是粘性标题,并且锚点位置似乎基于顶部,即使我在页面本身上工作的主 div 上有 css 填充但是当我点击一个锚点时它不能正常工作。
我输入了这个平滑的滚动代码,只要锚链接来自页面本身,它就可以正常工作,但是当我从另一个页面直接导航到锚时它没有任何效果
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') || location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: (target.offset().top - 120)
}, 1000);
return false;
}
}
});
任何人都可以帮我完成这项工作,因此无论点击是来自另一个页面还是来自锚点所在的页面内,都是一样的。
【问题讨论】: