2016-05-13 81 views
0

最好的方法,我使用jQuery + jQuery用戶界面,目前有以下代碼: -jQuery的 - 爲「滾動錨」的同一頁面和新的頁面

$(function() { 
    $('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) +']'); 
     if (target.length) { 
     $('html, body').animate({ 
      scrollTop: target.offset().top 
     }, 1000); 
     return false; 
     } 
    } 
    }); 
}); 

我得到的滾動效應,當滾動到是在同一頁面上,但如果我加載一個新頁面,它不會滾動到該部分,例如from/home/to/about /#section3

如何在同一頁面錨點上同時工作,並在加載新頁面時滾動到錨點?

+0

您是否嘗試過使用'$(window).load()'啓動平滑滾動? – ntgCleaner

+0

爲什麼downvote?真的不知道有時 – nsilva

+0

沒有一個好方法讓它在新頁面上滾動。您必須等待頁面完成加載後才能準確定位您希望滾動的位置,然後瀏覽器的默認跳轉到目標就已經發生。 –

回答

1

我無法發表評論(由於聲譽)。

你嘗試這種解決方案:Smooth scroll to anchor after loading new page

我用它的一個項目,它能正常工作。只看看答案的意見;)

+0

是的,這個工程,只需要改變從'/ about /#section'到'/ about#section'的鏈接(刪除額外的正斜槓) – nsilva