Jquery 在绝对容器内平滑滚动?

Jquery 在绝对容器内平滑滚动?,jquery,scroll,Jquery,Scroll,我正在尝试使用平滑滚动脚本来定位div中的锚。问题是链接没有针对正确的定位点。我觉得它可以用。想法 小提琴: 我基本上在使用这个脚本: $(function() { $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this

我正在尝试使用平滑滚动脚本来定位div中的锚。问题是链接没有针对正确的定位点。我觉得它可以用。想法

小提琴:

我基本上在使用这个脚本:

$(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;
      }
    }
  });
});
我找到了答案:

更新的小提琴:

参考资料:


一个更具体的问题可能会得到更具体的答案。
$(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;
      }
    }
  });
});