我正在尝试使用平滑的滚动脚本来定位div内的锚点。问题是链接没有针对正确的锚点。我有点工作。想法?
小提琴: http://jsfiddle.net/YtJcL/691/
我基本上使用这个脚本:
$(function() {
$('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) {
$('#wrapper').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
答案 0 :(得分:2)
我找到了答案:
更新小提琴: http://jsfiddle.net/YtJcL/694/
参考: Smooth scrolling within element, only first link/anchor works
$(function() {
$('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) {
$('#wrapper').animate({
scrollTop: $('#wrapper').scrollTop() + target.offset().top
}, 1000);
return false;
}
}
});
});