2017-04-25 193 views
0

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

例如,在下一页上,右侧边栏中的“发布作业”链接/按钮无效 - 单击链接不会执行任何操作 - 甚至不会打开页面。

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> 

回答

0

将滚动行为设置为仅在此位置工作:https://indianashrm.staging.wpengine.com/job-board/#createjobposting

#createjobposting窗体不在加载页面(https://indianashrm.staging.wpengine.com/job-board/graphic-designer/),但是当你在/job-board/它是。

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

enter image description here

考虑到没有工作拨弄例如,该解决方案可以是多样的,但有可能是:

  • 放置#createjobposting形式/graphic-designer/和 你有其他每个工作项目显示发布作业按钮(如果希望保留滚动行为,建议使用此按钮)。

  • ,或者使用一个完整的URL链接:https://indianashrm.staging.wpengine.com/job-board/#createjobposting(如果你是在其他地方,这将重新加载到该位置)

+0

所以你说的问题是与jQuery的功能。我没有专门设置平滑滚动以仅工作某些页面。我在其他页面上使用平滑滚动,但我现在才认识到这个问题。关于这两种选择,方案一是不可行的。我在代码中使用按钮的完整URL,其他内容正在剥离完整路径。我会寻找其他平滑的滚动解决方案 - 感谢Syden。 – Brett

+0

是的,当你在'/ graphic-designer /'中时,你的jQuery从'this.hash'偏移,比方说,将'this'变成'https://indianashrm.staging.wpengine.com/job-board/ graphic-designer /#createjobposting' - 并且表单在那里不存在,因此undefined显示出来。 – Syden