【问题标题】:How to scroll animate to specific ID minus X distance?如何将动画滚动到特定 ID 减去 X 距离?
【发布时间】:2015-05-12 14:00:19
【问题描述】:

我想滚动到特定的 div,然后让它在目标 div 之前大约 70 像素处停止。这是我弄下来的教程:

https://css-tricks.com/snippets/jquery/smooth-scrolling/

$(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) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});

【问题讨论】:

  • scrollTop: target.offset().top - 70?
  • 嗨@rjtkoh,如果您提到您遇到的问题/错误消息,这将改善您的问题 --- 或者至少解释什么不起作用。

标签: javascript jquery jquery-animate autoscroll


【解决方案1】:

从目标 div 顶部减去 70

$(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) {
            $('html,body').animate({
              scrollTop: target.offset().top - 70
            }, 1000);
            return false;
          }
    }
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 2018-01-07
    • 1970-01-01
    • 2015-10-11
    相关资源
    最近更新 更多