【问题标题】:Smooth Page Scroll - First click scrolls to wrong position. Second link works just fine平滑页面滚动 - 第一次单击滚动到错误的位置。第二个链接工作得很好
【发布时间】:2016-10-06 18:48:45
【问题描述】:

使用CSS Tricks Smooth Page Scroll 代码,我们刚刚注意到,当您第一次单击导航链接时,它会向下滚动,但不会滚动到应有的程度,大约比预期部分高 700 像素。任何后续点击都可以正常工作,这是奇怪的第一次导航点击。

我已经尝试过 window.load,因为我了解到,在用户第一次点击时,所有页面元素可能都没有加载,但这并没有帮助。

jQuery 版本:1.12.4/jquery.min.js

$(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;
      }
    }
  });
});

【问题讨论】:

  • 我也一样,怎么回事?

标签: javascript jquery css scroll smooth-scrolling


【解决方案1】:

我无法重现您所描述的问题。平滑滚动将转到您页面上的相应部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    相关资源
    最近更新 更多