2016-05-13 64 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