【问题标题】:One Page website hashbang navigation scrolling一页网站hashbang导航滚动
【发布时间】: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


【解决方案1】:

您应该使用 .htaccess 或您的等效服务器来处理 #! 的位置更改!到 #,或者更好的是,假设您使用 AngularJS,请启用 HTML5 模式。

【讨论】:

    猜你喜欢
    • 2015-08-23
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2015-07-07
    相关资源
    最近更新 更多