由于平滑滚动,传出链接无法打开

Outgoing links not opening because of smooth scrolling

对于我的锚点link,我设置了平滑滚动,这很有效。此外,每个传出的 link 都在工作,但导航中的传出 links 不起作用(参见图中的绿色箭头)。它产生了一个未捕获的引用错误。为什么会这样,如何解决?

问题是关于这个网站的:https://bm-translations.de/km.php

我一开始就遇到了所有 link 的问题,我可以用以下代码解决它:

// Captures click events of all <a> elements with href starting with #
$(document).on('click', 'a[href^="#"]', function (event) {

  // Click events are captured before hashchanges. Timeout
  // causes offsetAnchor to be called after the page jump.

  window.setTimeout(function () {
    // offsetAnchor();
  }, 0);
});

// Set the offset when entering page with hash present in the url
window.setTimeout(offsetAnchor, 0);

解决方案是 css :not() 选择器,用于从该函数中排除特定 class:

//Smooth scrolling when clicking an anchor link

$(document).on("click", ".navbar-nav a:not('.externallink')", function(event) {

  event.preventDefault();



  $("html, body").animate(

    {

      scrollTop: $($.prop(this, "hash")).offset().top

    },

    500

  );

});