2016-01-07 21 views
0

正如标题所说的锚链接,我发现this script得到平滑滚动和完善了一点,现在它看起来像这样:提高平滑滚动,以获取URL您的地址栏

$('a[href*=#]:not([href=#])').on('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) +']'); 
     if (target.length) { 
     $('html,body').animate({ 
      scrollTop: target.offset().top - 50 // in this case 50px BEFORE 
     }, 1000); 
     return false; 
     } 
    } 
    }); 

它触发当用户点击一个锚链接时。

的问题是,URL地址不会改变。我还想更改用户URL地址栏,以便每次用户单击让他滚动的链接时都会显示锚链接。

预先感谢任何帮助,您可以给我。

PS:你可以看到网站,我测试这个here

回答

0

您可以通过更改地址:

if (target.length) { 
    history.replaceState(null, null, this.hash); 
    $('html,body').animate({ 
     scrollTop: target.offset().top - 50 // in this case 50px BEFORE 
    }, 1000); 
    return false; 
} 

如果您想更新浏览器历史记录,使用来代替:

history.pushState(null, null, this.hash); 

More info

+0

非常感谢!这工作! :) – Santiago