平滑滚动 -

时间:2017-04-25 14:19:38

标签: jquery smooth-scrolling

我实现了以下代码以启用平滑滚动。问题是当它们不在书签的页面上时似乎不允许书签链接工作。

例如,在下一页上,右侧边栏中的“发布作业”链接/按钮不起作用 - 单击该链接不起作用 - 甚至不打开页面。

https://indianashrm.staging.wpengine.com/job-board/graphic-designer/

如果书签链接位于带书签的页面上,则可以使用 - 请参阅“在侧栏中发布作业链接/按钮。”

https://indianashrm.staging.wpengine.com/job-board/

有什么想法吗?

<script>
jQuery(document).ready(function($){
  // Add smooth scrolling to all links
  $("a").on('click', function(event) {

    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 800, function(){

        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    } // End if
  });
});
</script>

1 个答案:

答案 0 :(得分:0)

滚动行为设置为仅适用于此位置: https://indianashrm.staging.wpengine.com/job-board/#createjobposting

#createjobposting表单不在加载的页面(https://indianashrm.staging.wpengine.com/job-board/graphic-designer/)中,但是当您在/job-board/时它就是。

如果您打算使用this.hash之外的按钮滚动行为,请不要使用job-board,如果您在其他地方,它将会抛出未定义的内容。

enter image description here

考虑到没有工作小提琴的例子,解决方案可能是多种多样的,但可以是:

  • #createjobposting表单放在/graphic-designer/上 您拥有的所有其他工作项显示发布工作按钮(如果您希望保留滚动行为,建议使用此项)。

  • 或使用完整的网址链接:https://indianashrm.staging.wpengine.com/job-board/#createjobposting(如果您在其他地方,则会重新加载到该位置)