我有一个导航到各个部分的页面。每个人都为该页面提供了一个锚标记,因此有人可以轻松地重新访问该部分。我想要的是正常机制正常工作,而不是跳转到该部分(根据正常的浏览器行为),我希望它在那里滚动。
我已经实现了滚动效果很好我只是不知道如何保持哈希 URL 以e.preventDefault()
阻止这种情况发生。如果我删除此行,则页面会在滚动前闪烁。
$(".navigation a").click(function(e){
$(".navigation a").removeClass("active");
$(this).addClass("active");
if ($(this).attr("href").substring(0,1) == "#"){
e.preventDefault();
$('html, body').animate({
scrollTop: $($(this).attr("href")).offset().top
}, 1000);
}
});