【发布时间】:2020-05-10 02:32:14
【问题描述】:
我正在使用非常标准的平滑滚动 javascript 链接到我的 wordpress 网站中相同页面上的锚点,它工作正常。但是,在尝试打开另一个页面上的锚点时,我没有从链接/按钮得到响应。就像链接/按钮完全死了。如果我右键单击它并选择在新选项卡中打开,它工作正常。如果我从站点标题中删除有问题的 javascript,它也可以工作。任何人都可以建议冲突可能在哪里吗?对于那些可能不熟悉的人,这是有问题的代码:
$(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
});
});
【问题讨论】:
标签: javascript wordpress smooth-scrolling