3 回答

TA贡献1788条经验 获得超4个赞
禁用可行的链接使用 href = "javascript:void(0)" 如果你想使用 animate 和 scrollTop 使用 href = "javascript:void(0);" 如果您想进入页面中的特定部分,请在超链接中使用 href="#sectionid"

TA贡献1856条经验 获得超11个赞
谢谢为我努力过的人。我有办法克服这个障碍。但我确信它不是 SEO 友好的。
如果我们将 URL 设为 example.com/pageslug?id=sectionid
那么下面的功能可以帮助你。
$.urlParam = function(name){ // function to get the section id passed through URL
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
return results[1] || 0;
}
var sectionid = $.urlParam('id');
if(sectionid != null) {
$('html, body').animate({
scrollTop: $("#"+sectionid).offset().top - 100
}, 1000);
}
但是,如果有任何更好的答案和/或对 SEO 友好的答案,我们将不胜感激。

TA贡献1773条经验 获得超3个赞
为此,您可以在目标页面上使用动画功能,如果 url 具有部分 ID,则此功能将运行。例如,目标网址是
example.com/pageslug/#sectionid.
在“example.com/pageslug”页面上编写动画函数,如果要检查的条件是 url 包含部分 id。如果部分 id 存在,则动画函数将执行以显示该部分的内容。
它可以解决你的问题。
添加回答
举报