【发布时间】:2015-08-17 08:52:05
【问题描述】:
我计划为我的单页网站进行 SEO 优化。 我有一件困难的事情要做,那就是使用 hashbang 的滚动功能。当我从导航中单击 url 时,会平滑滚动到该页面上的部分。
<ul class="navigation" id="menu">
<li>
<a href="#personal-profile">Personal Details</a>
</li>
<li>
<a href="#work-experience">Experience</a>
</li>
<li>
<a href="#education">Education</a>
</li>
<li>
<a href="#skills">Skills</a>
</li>
<li>
<a href="#contact">Contact</a>
</li>
</ul>
我用这个函数来平滑滚动:
function handleSmoothScrolling() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
console.log(target.offset());
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 500);
return false;
}
}
});
}
当我使用默认网址时一切都很好,但是当我改变时
<a href="#work-experience">Experience</a> to <a href="#!work-experience">Experience</a>
并且部分 div 的 id 为 id="!work-experience" ,网站没有滚动,并且网络浏览器试图加载一个不存在的 url。
【问题讨论】:
-
应该!#是#!在href中?
-
嗨。应该是#!andSectionNameHere
-
我问是因为你的例子有!#
标签: javascript jquery html scrolltop