【问题标题】:Smooth scroll inside absolute container?在绝对容器内平滑滚动?
【发布时间】:2014-04-30 23:29:55
【问题描述】:

我正在尝试使用平滑滚动脚本定位 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;
      }
    }
  });
});

【问题讨论】:

  • 更具体的问题可能会得到更具体的答案。

标签: jquery scroll


【解决方案1】:

我找到了答案:

更新小提琴: 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;
      }
    }
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多