2016-01-07 20 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