【发布时间】:2017-04-25 14:19:38
【问题描述】:
我实现了以下代码来启用平滑滚动。问题是它似乎不允许书签链接不在书签页面上时工作。
例如,在以下页面上,右侧边栏中的“发布工作”链接/按钮不起作用 - 单击该链接没有任何作用 - 甚至无法打开页面。
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>
【问题讨论】: