【发布时间】:2012-03-28 00:27:02
【问题描述】:
我试图在一些页面中使用 jQuery 的页面滚动,并且可以成功地进行平滑的页面滚动。我现在唯一的问题是尝试从不同的页面执行此操作时。我的意思是如果我点击页面中的链接,它应该加载新页面,然后滚动到特定的 div 元素。
这是我用来在页面内滚动的代码:
var jump=function(e)
{
//prevent the "normal" behaviour which would be a "hard" jump
e.preventDefault();
//Get the target
var target = $(this).attr("href");
//perform animated scrolling
$('html,body').animate(
{
//get top-position of target-element and set it as scroll target
scrollTop: $(target).offset().top
//scrolldelay: 2 seconds
},2000,function()
{
//attach the hash (#jumptarget) to the pageurl
location.hash = target;
});
}
$(document).ready(function()
{
$('a[href*=#]').bind("click", jump);
return false;
});
我希望这个想法很清楚。
谢谢
非常重要的注意事项: 我在上面发布的这段代码在同一页面内工作得很好,但我所追求的是单击一个页面中的链接并转到另一个页面,然后滚动到目标。我希望现在很清楚。谢谢
【问题讨论】:
-
所以基本上这个想法是加载页面,我假设使用 ajax,然后平滑滚动到目标?比如你如何浏览acko.net上的页面?
-
是的,类似这样,但在单击按钮之前不要加载页面。意思是先点击 url 移动到另一个页面,然后在页面加载后,它向下滚动到 div 元素。看起来有人已经回答了。
标签: jquery scroll smooth-scrolling